首页 文章

如何更改android操作栏 Headers 和图标

提问于
浏览
206

我正试图在Android上的ActionBar上做一些事情 .

我已经在操作栏的右侧添加了新项目 .

如何更改操作栏的左侧?我想更改图标和文本,我想在操作栏中为其他屏幕添加“后退按钮”

Android Action Bar

15 回答

  • 3

    要使所有操作栏都可以使用单个图标,您可以在Android Manifest中执行此操作 .

    <application
        android:logo="@drawable/Image">
    
        ...
    
    </application>
    
  • 6

    如果要更改操作栏 Headers ,只需在活动的onCreate()中提供以下1行代码

    getActionBar().setTitle("Test");
    
  • 6

    您只需添加这3行代码即可 . 用您自己的图标替换图标 . 如果要生成图标,请使用this

    getSupportActionBar().setHomeAsUpIndicator(R.drawable.icon_back_arrow);
    getActionBar().setHomeButtonEnabled(true);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    
  • 5

    这很容易实现

    如果要在代码中更改它,请致电:

    setTitle("My new title");
    getActionBar().setIcon(R.drawable.my_icon);
    

    并将值设置为您喜欢的任何值 .

    或者,在Android清单XML文件中:

    <activity android:name=".MyActivity" 
           android:icon="@drawable/my_icon" 
           android:label="My new title" />
    

    要在您的应用中启用后退按钮,请使用:

    getActionBar().setHomeButtonEnabled(true);
     getActionBar().setDisplayHomeAsUpEnabled(true);
    

    代码应全部放在 onCreate 中,以便标签/图标更改对用户透明,但实际上它可以在活动的生命周期中的任何位置调用 .

  • 0

    您可以通过向相应的可绘制文件夹添加任何图标来更改您的图标,然后在AndroidManifest.xml文件中更改此行:

    android:icon="@drawable/ic_launcher"
    

    匹配你的图标名称 . 或者将您的图标设置为ic_launcher,如果它们是相同的图标 . 至于它的内容,添加或更改与res / values / strings.xml文件中的字符串匹配的任何字符串 . 然后,再次在AndroidManifest.xml文件中,更改以下行:

    android:label="@string/app_name"
    

    无论你在他们身上有什么字符串 . 您必须为整个应用程序执行此操作,以及您想要的任何活动,但行是相同的 .

    希望这可以帮助 .

  • 3

    在Android 5.0中,材料设计指南不鼓励在actionBar中使用图标

    启用它添加以下代码

    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setLogo(R.mipmap.ic_launcher);
    getSupportActionBar().setDisplayUseLogoEnabled(true);
    

    归功于article的作者

  • 9

    为此,您可以通过两种方式完成:XML或Java . 看这里:How to change the text on the action bar

    所以:

    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);
           }
     }
    }
    
  • 16

    For set Title

    getActionBar().setTitle("Title");
    

    For set Icon

    getActionBar().setIcon(R.drawable.YOUR_ICON_NAME);
    
  • 481
    ActionBar actionBar = getSupportActionBar();
    actionBar.setTitle(getString(R.string.titolo));
    actionBar.setIcon(R.mipmap.ic_launcher);
    actionBar.setDisplayShowHomeEnabled(true);
    
  • 2

    我在 onNavigationItemSelected 中使用了以下调用:

    HomeActivity.this.setTitle(item.getTitle());
    
  • 0

    在活动中的onCreate函数中添加以下代码 .

    setTitle("NewName");
    
  • 0

    默认情况下,操作栏 Headers 将使用当前活动的标签,但您也可以通过 ActionBar.setTitle() 以编程方式设置它 .

    要实现您正在讨论的"Back"(更准确地说,"Up")按钮功能,请阅读Action Bar developer guide的"Using the App Icon for Navigation"部分 .

    最后,为了更改图标,指南也涵盖了这一点 . 简而言之,操作栏将在清单的 applicationactivity 元素中显示 android:icon 中提供的图像(如果有) . 典型的做法是创建一个名为 ic_launcher.png 的应用程序图标(以您需要的所有密度),并将其放在 drawable-* 目录中 .

  • 1

    我收到了 non-static method setTitle(CharSequence) cannot be referenced from a static context 错误,因为我在静态PlaceholderFragment类中使用了 setTitle() . 我用 getActivity().getActionBar().setTitle("new title"); 解决了

  • 0

    转到要更改的特定活动的清单操作栏 Headers 名称并写入android:label =“ Headers 名称”

  • 9

    这项工作对我来说:

    getActionBar().setHomeButtonEnabled(true);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeAsUpIndicator(R.mipmap.baseline_dehaze_white_24);
    

相关问题