我正在尝试在Android的动作栏上做一些事情。
我已经在操作栏的右侧添加了新项目。
如何更改操作栏的左侧?我想改变图标和文本,我想在其他屏幕的操作栏中添加一个“返回按钮”
我正在尝试在Android的动作栏上做一些事情。
我已经在操作栏的右侧添加了新项目。
如何更改操作栏的左侧?我想改变图标和文本,我想在其他屏幕的操作栏中添加一个“返回按钮”
当前回答
为此,可以采用两种方式:XML或Java。看这里:如何更改操作栏上的文本
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);
}
}
}
其他回答
要让一个图标可以被你所有的动作栏使用,你可以在你的Android Manifest中这样做。
<application
android:logo="@drawable/Image">
...
</application>
进入AndroidManifest.xml文件。 找到<application>标记 在这里您可以看到一个属性
android:label="@string/app_name"
现在转到res > values > strings.xml
改变
<string name="app_name">MainActivity</string>
to
<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>
转到您想要更改操作栏标题名称并写入的特定活动的清单 android: label = "标题名称"
默认情况下,操作栏标题将使用当前活动的标签,但您也可以通过ActionBar.setTitle()以编程方式设置它。
要实现你所说的“后退”(更准确地说,“向上”)按钮功能,请阅读操作栏开发者指南中的“使用应用程序图标导航”部分。
最后,要更改图标,指南也包括了这些内容。简而言之,操作栏将显示在你的manifest的应用程序或活动元素中提供的android:icon的图像(如果有的话)。典型的做法是创建一个名为ic_launcher.png的应用程序图标(在您需要的所有不同密度中),并将其放置在您的drawable-*目录中。
你可以在你的可绘制文件夹中添加任何你想要的图标,然后在你的AndroidManifest.xml文件中更改这一行:
android:icon="@drawable/ic_launcher"
来匹配你的图标的名字。或者把你的图标作为ic_launcher,如果它们是相同的图标。至于它说什么,添加或改变任何字符串匹配的res/values/strings.xml文件。然后,再次在AndroidManifest.xml文件中修改这一行:
android:label="@string/app_name"
到他们的字符串中。您必须为整个应用程序以及您想要的任何活动执行此操作,但行是相同的。
希望这能有所帮助。