5

私はアンドロイドが初めてです.3つのタブを持つ1つのアクションバーを実装しようとしています.各タブには1つのアイコンとタブの名前が含まれています.各タブにアイコンとテキストを配置することに成功しましたが、残念ながらアイコンはテキストの左側にあります. (タブの名前) タブで。左側ではなくテキストの上部にアイコンを配置したい。私のコードのスニペットを見つけて、解決策を見つけるのを手伝ってください。前もって感謝します、

        private void setActionBar()

         {               

           ActionBar bar = getActionBar();

    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayShowHomeEnabled(false);
    bar.setDisplayShowTitleEnabled(false);

    ActionBar.Tab tabA = bar.newTab().setText("TabA");
        tabA.setIcon(R.drawable.iconA);

            ActionBar.Tab tabB = bar.newTab().setText("TabB");
    tabB.setIcon(R.drawable.iconB);

    ActionBar.Tab tabC = bar.newTab().setText("TabC");
            tabC.setIcon(R.drawable.iconC);
        }
4

1 に答える 1

5

カスタム ビューを使用して、タブの表示方法を定義できます。

  1. テキストの上に画像があるカスタムレイアウトを定義します
  2. アクティビティで、ビューを膨らませ、画像とテキストの値を設定します
  3. タブのカスタム ビューを設定する

大まかな例を次に示します。

カスタムレイアウト

<?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:gravity="center_horizontal"
    android:orientation="vertical" >

<ImageView
    android:id="@+id/tabIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:paddingTop="2dp" />

<TextView
    android:id="@+id/tabText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:textColor="#FFFFFF" />

</LinearLayout>

ビューを膨らませる

View tabView = activity.getLayoutInflater().inflate(R.layout.actiobar_tab, null);
TextView tabText = (TextView) tabView.findViewById(R.id.tabText);
tabText.setText(R.String.sometext);

ImageView tabImage = (ImageView) tabView.findViewById(R.id.tabIcon);
tabImage.setImageDrawable(activity.getResources().getDrawable(R.drawable.someimage));

特定のタブのカスタム ビューを設定する

Tab tab = actionBar.newTab().setCustomView(tabView)
于 2012-10-22T02:29:14.330 に答える