如何更改android的动作条标题和图标(How do I change the android a

2019-07-18 14:49发布

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

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

我怎样才能改变操作栏的左侧? 我想更改图标和文字,我想在操作栏中添加一个“后退按钮”,为其他屏幕

Answer 1:

这是很容易做到

如果你想改变它的代码,请拨打:

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 ,使标签/图标变化是对用户透明的,但实际上,它可以在任何地方的活动的生命周期中被调用。



Answer 2:

为了使一个图标是可用的所有动作条,你可以在你的Android清单做到这一点。

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

    ...

</application>


Answer 3:

你只需要添加这3行代码。 替换为自己的图标上。 如果你想生成图标使用此

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


Answer 4:

如果你想改变操作栏标题只是给下面的1号线在OnCreate()你的活动代码

getActionBar().setTitle("Test");


Answer 5:

在安卓5.0的材料设计指南劝阻ActionBar中使用的图标

使其能够添加以下代码

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

归功于该作者的文章



Answer 6:

您可以通过添加任何你想要的图标,使各自绘制的文件夹,然后改变这一行的AndroidManifest.xml文件改变你的图标:

android:icon="@drawable/ic_launcher"

匹配无论你的图标的名称是在那里。 或者把你的图标,ic_launcher,如果它们是相同的图标。 至于什么它说,添加或更改任何字符串你RES /价值/ strings.xml档案匹配了这一点。 然后,再一次在你的AndroidManifest.xml文件,改变这一行:

android:label="@string/app_name"

到任何字符串你必须在他们的。 你必须为应用程序作为一个整体,无论和活动,你要做到这一点,但线是相同的。

希望这可以帮助。



Answer 7:

对于这一点,你可以做到这一点在两个方面:XML或Java。 在这里看到: 如何更改操作栏上的文字

所以:

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


Answer 8:

ActionBar actionBar = getSupportActionBar();
actionBar.setTitle(getString(R.string.titolo));
actionBar.setIcon(R.mipmap.ic_launcher);
actionBar.setDisplayShowHomeEnabled(true);


Answer 9:

对于集标题

getActionBar().setTitle("Title");

对于设置图标

getActionBar().setIcon(R.drawable.YOUR_ICON_NAME);


Answer 10:

我用里面以下调用onNavigationItemSelected

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


Answer 11:

添加下面的代码在你的活性的onCreate函数中。

setTitle("NewName");


Answer 12:

这项工作对我来说:

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


Answer 13:

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

要实现你在谈论的“返回”(更准确地说,“向上”)按钮功能,阅读的“使用应用程序图标的导航”部分操作栏开发人员指南 。

最后,更改图标,该指南涵盖这一点。 总之,操作栏将显示在提供的图像android:icon在清单的applicationactivity元素,如果有一个。 典型的做法是创建一个应用程序图标(在所有你所需要的各种密度的)命名ic_launcher.png ,并把它放在你的drawable-*目录。



Answer 14:

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



Answer 15:

转到体现在哪些具体活动要更改操作栏标题的名字,写的android:标签=“标题名称”



Answer 16:

转到AndroidManifest.xml文件。 找到<application>标签那里你可以看到一个属性

android:label="@string/app_name"

现在去水库>值>的strings.xml

更改

<string name="app_name">MainActivity</string> 

<string name="app_name">Your Desired Name</string>

AndroidManifest.xml中

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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=".SubmitForm">

        </activity>
    </application>

strings.xml中

<resources>
    <string name="app_name">Your Desired Name</string>
    <string name="action_settings">Settings</string>
</resources>


文章来源: How do I change the android actionbar title and icon