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