繁体   English   中英

如何更改android中选项卡的背景颜色?

[英]How can i change the background color for tabs in android?

我有标签工作。 我想更改标签的背景颜色或主题。 当我单击选项卡时,默认颜色显示为灰色,但我想以自定义颜色显示该选项卡。

这是我的代码:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"

     >
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
               >

        >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="-30dp"
            android:background="#FF0000"

            />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
             >

        </FrameLayout>
    </LinearLayout>

</TabHost>

类文件是

public class AndroidtabActivity extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
//        View title = getWindow().findViewById(android.R.id.title);
//        View titleBar = (View) title.getParent();
//        titleBar.setBackgroundResource(R.drawable.top_bar2);


       Resources res = getResources();
        TabHost tabHost = getTabHost();
        //tabHost = (TabHost) findViewById(R.id.tabhost);
        tabHost.setup();
        //tabHost.setBackgroundResource(R.drawable.pink);


        TabHost.TabSpec spec; 
        //tabHost.getTabWidget().setDividerDrawable(R.drawable.top_bar);
        Intent in;
        in = new Intent().setClass(this, MainActivity.class);
        spec=tabHost.newTabSpec("calc").setIndicator("calculate").setContent(in);
        tabHost.addTab(spec);
        in = new Intent().setClass(this, TutorialZoomActivity1.class);
        spec=tabHost.newTabSpec("help").setIndicator("help").setContent(in);
        tabHost.addTab(spec);
        tabHost.setCurrentTab(2);
       // spec1.setIndicator("Tab 1",getResources().getDrawable(R.drawable.flash));




    }
}

请告诉我如何更改标签的背景颜色。

您可以通过以下方式更改标签的背景颜色

for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
        tabHost.getTabWidget().getChildAt(i).
                                   setBackgroundResource(R.drawable.hans_layout_nblue);
    }

也许您应该创建自定义标签。

  1. 让我们说说..创建另一个custom_tab.xml (linearlayout和textview)
  2. 在linearlayout中,将android:background设置为drawable中的另一个xml文件(@ drawable / tab_selector)
  3. 在上面提到的drawable中创建新的xml文件。 例如 tab_selector.xml 在此文件中,您可以设置状态(聚焦,按下,选中等)

    祝好运

暂无
暂无

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

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