How to change android Activity label

前端 未结 10 1659
心在旅途
心在旅途 2021-02-05 02:53

I have created an Activity and declared in Manifest file. But I would like to re-use the same Activity for other purpose.

 

        
相关标签:
10条回答
  • 2021-02-05 03:06

    You need to use setTitle for this:

    setTitle(R.string.your_title);
    //or
    setTitle("new title");
    
    0 讨论(0)
  • 2021-02-05 03:07

    In your Manifest file

    Initially, your code was like this.

        <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".NumbersActivity"  />
        <activity android:name=".FamilyMembersActivity"  />
        <activity android:name=".ColorsActivity"  />
        <activity android:name=".PhrasesActivity" />
        </activity>
    </application>
    

    But after changes to add Labels.

    <activity android:name=".NumbersActivity" android:label="Numbers" />
        <activity android:name=".FamilyMembersActivity" android:label="Family Members" />
        <activity android:name=".ColorsActivity" android:label="Colors" />
        <activity android:name=".PhrasesActivity" android:label="Phrases" >
        </activity>
    </application>
    

    is the optimum way to change the Label name.

    courtesy.(ryanwaite28)

    0 讨论(0)
  • 2021-02-05 03:08
    public class YourActivity extends Activity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
          setTitle(R.string.your_title);
          setContentView(R.layout.main);
      }
    }
    
    0 讨论(0)
  • 2021-02-05 03:08

    You can define the string in res/string.xml file and then add android:label to the AndroidMenifest.xml file

    string.xml code
    <string name="string_name">text to display</string>
    
    AndroidMenifest.xml code
    
    <activity android:name=".SomeActivity"
                android:label="@string/string_name"/>
    
    0 讨论(0)
  • 2021-02-05 03:16

    I have the same problem, Try this in onCreate it's work for me!

    public class YourActivityName extends Activity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
        setTitle("Your Title");
      }
    }
    
    0 讨论(0)
  • 2021-02-05 03:16

    android:label="any word you want"/>

    0 讨论(0)
提交回复
热议问题