How do I change the android actionbar title and ic

2019-01-03 00:53发布

I'm trying to do some things on the ActionBar in Android.

I've already added new items in the right side of the action bar.

How can I change the left side of the action bar? I want to change the icon and the text, and I want to add a "Back Button" in the action bar for the other screens

Android Action Bar

15条回答
孤傲高冷的网名
2楼-- · 2019-01-03 01:21

Go to manifest in which specific activity you want to change Action bar Title name and write android:label="Title name"

查看更多
你好瞎i
3楼-- · 2019-01-03 01:23

If you want to change the Action bar title just give the following 1 line code in the onCreate() of your Activity

getActionBar().setTitle("Test");
查看更多
地球回转人心会变
4楼-- · 2019-01-03 01:27

For that, you can do it in 2 ways: XML or Java. See here: How to change the text on the action bar

So:

XML:

<activity android:name=".Hello_World"
              android:label="This is the Hello World Application">
</activity>

Java:

public class TitleBar extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

       setContentView(R.layout.main);


       if ( customTitleSupported ) {
           getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
           }

       final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
       if ( myTitleText != null ) {
           myTitleText.setText("NEW TITLE");

           // user can also set color using "Color" and then "Color value constant"
          // myTitleText.setBackgroundColor(Color.GREEN);
       }
 }
}
查看更多
▲ chillily
5楼-- · 2019-01-03 01:27

For set Title :

getActionBar().setTitle("Title");

For set Icon :

getActionBar().setIcon(R.drawable.YOUR_ICON_NAME);
查看更多
贪生不怕死
6楼-- · 2019-01-03 01:29

You just need to add these 3 lines of code. Replace the icon with your own icon. If you want to generate icons use this

getSupportActionBar().setHomeAsUpIndicator(R.drawable.icon_back_arrow);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
查看更多
在下西门庆
7楼-- · 2019-01-03 01:31

I used the following call inside onNavigationItemSelected:

HomeActivity.this.setTitle(item.getTitle());
查看更多
登录 后发表回答