簡體   English   中英

如何在android中更改選項卡指示器顏色

[英]How to change tab indicator color in android

我想在 android 中更改選項卡指示器顏色。 Android 應用中標簽主機的默認顏色為藍色。 我想換成其他顏色。 請幫我。

我的代碼如下:

`

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="-4dp"
        android:layout_weight="0" />
    <View 
        android:layout_width="fill_parent"
        android:background="@color/btn_color"
        android:layout_height="5dp"/>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />

    <Button
        android:id="@+id/buttonBookConfirm"
        android:layout_width="fill_parent"
        android:layout_height="35dp"
        android:textColor="@color/btn_text"
        android:background="@color/btn_color"
        android:layout_alignParentBottom="true"
        android:text="@string/btn_text" />
</LinearLayout>

`

我的java代碼如下: ` public class TabHostActivity extends TabActivity { /** 在首次創建活動時調用。 */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tab_host);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost(); // The activity TabHost
    TabHost.TabSpec spec; // Reusable TabSpec for each tab
    Intent intent; // Reusable Intent for each tab
    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(TabHostActivity.this,HomeActivity.class);
    spec = tabHost.newTabSpec("home")
            .setIndicator("", res.getDrawable(R.drawable.plays))
            .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs

    intent = new Intent().setClass(TabHostActivity.this, AboutActivity.class);
    spec = tabHost.newTabSpec("about")
            .setIndicator("", res.getDrawable(R.drawable.movies))
            .setContent(intent);
    tabHost.addTab(spec);


    intent = new Intent().setClass(TabHostActivity.this, ContactActivity.class);
    spec = tabHost
            .newTabSpec("contact")
            .setIndicator("",
                    res.getDrawable(R.drawable.events))
            .setContent(intent);
    tabHost.addTab(spec);

    //set tab which one you want open first time 0 or 1 or 2
    tabHost.setCurrentTab(0);

}`

我附上了同樣的圖片。 我只想更改所選選項卡下方的藍色線條。


在此處輸入圖片說明

android:setLeftStripDrawable="@color/your_custom_color

在 res->values 文件夾中創建自定義顏色資源並將其引用到那里

您應該查看TabWidget文檔 您必須使用android:tabStripLeftandroid:tabStripRight XML 屬性來更改背景顏色。

最簡單的方法可能是使用您想要的顏色創建一個 1px x 1px png 並將其放置在您drawable資產目錄中,然后使用它。

只需使用app:tabIndicatorColor="@color/your color"

您可以使用以下方法以編程方式實現此目的:

TabLayout tabLayout = findViewById(R.id.YOUR_TAB_LAYOUT);

tabLayout.setSelectedTabIndicatorColor(YOUR_COLOR)); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM