[英]How to change tab indicator color in android
I want to change tab indicator color in android.我想在 android 中更改选项卡指示器颜色。 Default color for tab host in android app is blue. Android 应用中标签主机的默认颜色为蓝色。 I want to change to some other color.我想换成其他颜色。 Please help me.请帮我。
My code is as follows:我的代码如下:
` `
<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>
` `
My java code is as follows: ` public class TabHostActivity extends TabActivity { /** Called when the activity is first created.我的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);
}`
I have attach image for the same.我附上了同样的图片。 I just want to change blue color line present below selected tab.我只想更改所选选项卡下方的蓝色线条。
android:setLeftStripDrawable="@color/your_custom_color
在 res->values 文件夹中创建自定义颜色资源并将其引用到那里
You should look a at the docs for TabWidget .您应该查看TabWidget的文档。 You have to use the android:tabStripLeft
and android:tabStripRight
XML properties to change the background color.您必须使用android:tabStripLeft
和android:tabStripRight
XML 属性来更改背景颜色。
Easiest way would probably be to crate an 1px x 1px png with the color you want and place it in your drawable
assets directory in your project, and use that.最简单的方法可能是使用您想要的颜色创建一个 1px x 1px png 并将其放置在您drawable
资产目录中,然后使用它。
只需使用app:tabIndicatorColor="@color/your color"
You can achieve this programatically using:您可以使用以下方法以编程方式实现此目的:
TabLayout tabLayout = findViewById(R.id.YOUR_TAB_LAYOUT);
tabLayout.setSelectedTabIndicatorColor(YOUR_COLOR));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.