简体   繁体   English

android更改选定的选项卡背景颜色

[英]android change selected tab background color

I came from objective-c and I am an Android newbie. 我来自objective-c,我是一个Android新手。 I am using following method that intends to change tabColor for index 0. But I would like to change default grey tab when selected. 我正在使用以下方法来改变tabColor的索引0.但我想在选中时更改默认的灰色选项卡。 Thank you. 谢谢。

mTabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.CYAN);

Use setOnTabChangedListener(TabHost.OnTabChangeListener l) on the TabHost: 在TabHost上使用setOnTabChangedListener(TabHost.OnTabChangeListener l):

myTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener(){
  @Override
  public void onTabChanged(String tabId) {
    int tab = myTabHost.getCurrentTab();
    myTabHost.getTabWidget().getChildAt(tab).setBackgroundColor(Color.CYAN);
  }
});

Maybe there is a simpler way, i dont have use it before ;) 也许有一种更简单的方法,我之前没有使用它;)

Android allows for a StateList drawable xml file that is the intended way to get the effect you are after. Android允许使用StateList可绘制的xml文件,这是获得后续效果的预期方式。

read about it here 在这里阅读它

The idea is you make an xml file that declares a different drawable (or color if you want plain colors) for each state. 我们的想法是创建一个xml文件,为每个状态声明一个不同的drawable(或颜色,如果你想要纯颜色)。 Then when you are apply that statelist drawable as the background of your View, and it will handle the "magic" of switching your view image for you so that you don't have to worry about doing it manually from java code. 然后,当您将状态列表drawable应用为View的背景时,它将处理为您切换视图图像的“魔力”,这样您就不必担心从java代码手动执行。

so your code snippet would look something like this: 所以你的代码片段看起来像这样:

myTabHost.getTabWidget().getChildAt(tab).setBackgroundResource(R.drawable.your_state_list_filename);

Here is an example of a state list file that I've used on a button. 这是我在按钮上使用的状态列表文件的示例。 You can copy this into an xml file in your drawables folder, then modify it to use whichever states and images you want. 您可以将其复制到drawables文件夹中的xml文件中,然后将其修改为使用您想要的任何状态和图像。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_pressed="true"
       android:drawable="@drawable/darkblue1" /> <!-- pressed -->
 <item android:state_focused="true"
       android:drawable="@drawable/darkblue1" /> <!-- focused -->
 <item android:drawable="@drawable/lightblue1" /> <!-- default -->
 </selector>

I think (but am not certain) that to use colors instead of drawables you'd just change "@drawable/blahblah" to "#FF121212" where the first two digits are alpha, and the next 6 are hex value for the color you want. 我认为(但不确定)使用颜色而不是drawables你只需将"@drawable/blahblah"更改为"#FF121212" ,其中前两位数字是alpha,接下来的6位是颜色的十六进制值想。

I just changed the markup of the TabHost 我刚刚更改了TabHost的标记

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
        android:background="@android:color/transparent">

I have used this one to solve my problem: 我用这个来解决我的问题:

    tabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            tabs.setSelectedIndicatorColors(Color.RED);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM