AndroidManifest中的android:label问题(无法设置标题)

问题描述 投票:1回答:2

[当我尝试为每个活动在AndroidManifest.xml中给不同的标题时,应用会显示所有子活动的第一个标题,而不是给定标题。我是编码方面的新手,很抱歉为我的问题提供了错误的解释。代码如下:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.android.miwok">

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name=".NumbersActivity"
                android:label="@string/category_numbers" />
            <activity
                android:name=".FamilyActivity"
                android:label="@string/category_family" />
            <activity
                android:name=".ColorsActivity"
                android:label="@string/category_colors" />
            <activity
                android:name=".PhrasesActivity"
                android:label="@string/category_phrases" />
        </application>

    </manifest>

我该如何解决?

更新:为了更加清楚,我正在添加我的MainActivity代码:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the content of the activity to use the activity_main.xml layout file
        setContentView(R.layout.activity_main);
        TextView numbers = (TextView) findViewById(R.id.numbers);
        TextView colors = (TextView) findViewById(R.id.colors);
        TextView family = (TextView) findViewById(R.id.family);
        TextView phrases = (TextView) findViewById(R.id.phrases);

        assert numbers != null;
        numbers.setOnClickListener(this);
        assert colors != null;
        colors.setOnClickListener(this);
        assert family != null;
        family.setOnClickListener(this);
        assert phrases != null;
        phrases.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.numbers:
                Intent numberIntent = new Intent(MainActivity.this,NumbersActivity.class);
                startActivity(numberIntent);
                break;
            case R.id.colors:
                Intent colorsIntent = new Intent(MainActivity.this,NumbersActivity.class);
                startActivity(colorsIntent);
                break;
            case R.id.family:
                Intent familyIntent = new Intent(MainActivity.this,NumbersActivity.class);
                startActivity(familyIntent);
                break;
            case R.id.phrases:
                Intent phrasesIntent = new Intent(MainActivity.this,NumbersActivity.class);
                startActivity(phrasesIntent);
                break;
        }

    }
}

第二次更新:当我将MainActivity.java更改为此时,一切都可以正常运行]

/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.android.miwok;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the content of the activity to use the activity_main.xml layout file
        setContentView(R.layout.activity_main);

        // Find the View that shows the numbers category
        TextView numbers = (TextView) findViewById(R.id.numbers);

        // Set a click listener on that View
        numbers.setOnClickListener(new OnClickListener() {
            // The code in this method will be executed when the numbers category is clicked on.
            @Override
            public void onClick(View view) {
                // Create a new intent to open the {@link NumbersActivity}
                Intent numbersIntent = new Intent(MainActivity.this, NumbersActivity.class);

                // Start the new activity
                startActivity(numbersIntent);
            }
        });

        // Find the View that shows the family category
        TextView family = (TextView) findViewById(R.id.family);

        // Set a click listener on that View
        family.setOnClickListener(new OnClickListener() {
            // The code in this method will be executed when the family category is clicked on.
            @Override
            public void onClick(View view) {
                // Create a new intent to open the {@link FamilyActivity}
                Intent familyIntent = new Intent(MainActivity.this, FamilyActivity.class);

                // Start the new activity
                startActivity(familyIntent);
            }
        });

        // Find the View that shows the colors category
        TextView colors = (TextView) findViewById(R.id.colors);

        // Set a click listener on that View
        colors.setOnClickListener(new OnClickListener() {
            // The code in this method will be executed when the colors category is clicked on.
            @Override
            public void onClick(View view) {
                // Create a new intent to open the {@link ColorsActivity}
                Intent colorsIntent = new Intent(MainActivity.this, ColorsActivity.class);

                // Start the new activity
                startActivity(colorsIntent);
            }
        });

        // Find the View that shows the phrases category
        TextView phrases = (TextView) findViewById(R.id.phrases);

        // Set a click listener on that View
        phrases.setOnClickListener(new OnClickListener() {
            // The code in this method will be executed when the phrases category is clicked on.
            @Override
            public void onClick(View view) {
                // Create a new intent to open the {@link PhrasesActivity}
                Intent phrasesIntent = new Intent(MainActivity.this, PhrasesActivity.class);

                // Start the new activity
                startActivity(phrasesIntent);
            }
        });
    }
}
whats the point?
android android-manifest
2个回答
0
投票

您可以使用代码命名:通过

actionbar.setTitle(“标题在这里”);

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (!MyApplication.getInstance().isTablet())
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    setContentView(R.layout.activity_settings);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(getString(R.string.settings));

    ....
}

0
投票

因此,当我从课程示例中复制MainActivity.java时,所有标题都开始正常工作。区别在于,我使用了“ switch(view.getId())”,而不是为每个活动创建新的onClick。我仍然不明白为什么它不起作用,但是希望我会从更多的经验中学到它。摘要:使用“ switch”语句进行活动可能会导致儿童活动标题出现问题(对于像我这样不了解的人)。

© www.soinside.com 2019 - 2024. All rights reserved.