-1

アクティビティにMenuItemがあります。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;
} 

ここに画像の説明を入力してください

ここに画像の説明を入力してください

4

2 に答える 2

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

}

文字列.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マニフェストで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 に答える