OptionsItem - 更改标题中的默认标题

时间:2013-01-13 13:25:52

标签: android android-layout android-preferences android-optionsmenu

我的Activity中有一个MenuItem。当我点击我的菜单中的任何一个菜单时,它会触发相关的活动。但是当我点击aboutus菜单时,弹出窗口将标题显示为MainActivity。如何改变此标题MainActivity?

aboutus.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="checking...." />

public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch(item.getItemId()) {
        case R.id.aboutus:
            Intent i = new Intent("android.intent.action.ABOUTUS");
            startActivity(i);
            break;
            case R.id.preferences:
                Intent p = new Intent("android.intent.action.PREFERENCES");
                startActivity(p);
                break;

    }
    return false;
} 

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:2)

更改此标题 MainActivity 就像这些方式一样。

正常方式,

您可以通过设置Android:label

来更改每个屏幕的标题(即活动)
   <activity android:name=".Hello_World"
                  android:label="This is the Hello World Application">
   </activity>

自定义 - 标题 - 栏

但如果您想以自己的方式自定义标题栏,即想要放置图标图标和自定义文本,那么以下代码正在为我运行:

<强> 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"
    >

</LinearLayout>

<强> titlebar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="400px" 
  android:layout_height="fill_parent"
  android:orientation="horizontal">

<ImageView android:id="@+id/ImageView01" 
            android:layout_width="57px" 
            android:layout_height="wrap_content"
            android:background="@drawable/icon1">

</ImageView>

<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>

<强> TitleBar.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);
           }
   }

}

<强>的strings.xml

strings.xml 文件在values文件夹下定义。

<?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>

答案 1 :(得分:0)

你可以在Android Manifest中使用android:label属性

android:label="This is the Hello World Application"

你也动态设置它。 使用所需的布局创建xml布局titlebar.xml。

@Override
public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       final boolean titleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
       setContentView(R.layout.main);

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