繁体   English   中英

Android的TabLayout,自定义背景色

[英]TabLayout Android, customize background color

在android教程中,无法自定义标签背景。 我的应用是基于官方教程的..有人可以为我提供有关backgroundcolor white的教程或代码吗? 这是我的代码:

LauncherActivity.java

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.launcher);

        Resources res = getResources(); 
        TabHost tabHost = getTabHost(); 
        TabHost.TabSpec spec; 
        Intent intent;  



        intent = new Intent().setClass(this, MyActivity.class);



        spec = tabHost.newTabSpec("myactivity").setIndicator("Activity")

                      .setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs
        intent = new Intent().setClass(this, search.class);
        spec = tabHost.newTabSpec("search").setIndicator("Search"
                          )
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, MyActivity3.class);
        spec = tabHost.newTabSpec("songs").setIndicator("Songs"
                          )
                      .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);
    }

Launcher.xml

   <?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="[url]http://schemas.android.com/apk/res/android"
   android:id="@android:id/tabhost"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
    <LinearLayout
       android:orientation="vertical"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:padding="5dp">
        <TabWidget
           android:id="@android:id/tabs"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"/>
        <FrameLayout
           android:id="@android:id/tabcontent"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:padding="0dp" />
    </LinearLayout>
</TabHost>

如果您打算更改标签的背景颜色,那么下面是代码:

for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
{   
      tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.WHITE); //unselected tab
}

对于所选标签,

tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.GRAY); // selected tab

您可以将上述行放入方法中(例如setTabColors())并调用它:

tabHost.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String arg0) {

               setTabColors(tabHost);
        }
});

暂无
暂无

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

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