更新:最新的 ActionBar(标题)模式:
FYI, 操作栏 http://developer.android.com/reference/android/app/ActionBar.html在 API Level 11 中引入。ActionBar 是 Activity 顶部的一个窗口功能,可以显示活动标题、导航模式和其他交互式项目(例如搜索)。
我清楚地记得自定义标题栏并使其在整个应用程序中保持一致。所以我可以和之前做一个比较,列出使用ActionBar的一些优点:
- 它为您的用户提供了跨应用程序的熟悉界面,系统可以优雅地适应不同的屏幕配置。
- 开发人员不需要编写太多代码来显示活动标题、图标和导航模式,因为 ActionBar 已经准备好顶级抽象。
例如:
=>正常方式,
getActionBar().setTitle("Hello world App");
getSupportActionBar().setTitle("Hello world App"); // provide compatibility to all the versions
=> 自定义操作栏,
例如:
@Override
public void setActionBar(String heading) {
// TODO Auto-generated method stub
com.actionbarsherlock.app.ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.title_bar_gray)));
actionBar.setTitle(heading);
actionBar.show();
}
设置操作栏的样式:
ActionBar 为您提供基本且熟悉的外观、导航模式和其他要执行的快速操作。但这并不意味着它在每个应用程序中看起来都一样。您可以根据您的 UI 和设计要求对其进行自定义。您只需定义和编写样式和主题。
阅读更多内容:设置操作栏的样式 https://developer.android.com/training/basics/actionbar/styling.html
如果你想为 ActionBar 生成样式,那么这个风格生成器 http://jgilfelt.github.io/android-actionbarstylegenerator/工具可以帮助你。
=================================================== ===============================
旧: 早期:
=>正常方式,
您可以通过设置其来更改每个屏幕的标题(即活动)Android:label
<activity android:name=".Hello_World"
android:label="This is the Hello World Application">
</activity>
=> 自定义 - 标题 - 栏
但是,如果您想以自己的方式自定义标题栏,即Want to put Image icon and custom-text
,那么以下代码对我有用:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
标题栏.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="400dp"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageView android:id="@+id/ImageView01"
android:layout_width="57dp"
android:layout_height="wrap_content"
android:background="@drawable/icon1"/>
<TextView
android:id="@+id/myTitle"
android:text="This is my new title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/titletextcolor"
/>
</LinearLayout>
标题栏.java
public class TitleBar extends Activity {
@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);
}
}
}
字符串.xml
strings.xml 文件定义在values
folder.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, Set_Text_TitleBar!</string>
<string name="app_name">Set_Text_TitleBar</string>
<color name="titlebackgroundcolor">#3232CD</color>
<color name="titletextcolor">#FFFF00</color>
</resources>