-1

我的活动中有一个 MenuItem。当我单击 Menuitem 中的任何一个菜单时,它会触发相关的 avtivity。但是当我单击 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;
} 

在此处输入图像描述

在此处输入图像描述

4

2 回答 2

2

改变这个标题MainActivity就像这很多方法一样。

正常方式,

您可以通过设置它们来更改每个屏幕的标题(即活动)Android:label

   <activity android:name=".Hello_World"
                  android:label="This is the Hello World Application">
   </activity>

自定义 - 标题 - 栏

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

主要的.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>

标题栏.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>

标题栏.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);
           }
   }

}

字符串.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>
于 2013-01-13T13:40:31.730 回答
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);
       }
}
于 2013-01-13T13:38:41.590 回答