在android中设置选项卡颜色,分隔符和文本对齐方式

时间:2014-12-22 10:43:14

标签: android fragment android-tabhost android-fragmentactivity

如何在tabhost小部件中设置文本对齐和分隔颜色。

mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);


    Bundle b = new Bundle();
    b.putString("key", "Category");
    mTabHost.addTab(mTabHost.newTabSpec("Category").setIndicator("Category"),
            CategoryFragment.class, b);

    b = new Bundle();
    b.putString("key", "Activity");
    mTabHost.addTab(mTabHost.newTabSpec("Activity")
            .setIndicator("Activity"), ActivityFragment.class, b);

    b = new Bundle();
    b.putString("key", "Chart");
    mTabHost.addTab(mTabHost.newTabSpec("Chart").setIndicator("Chart"),
            ChartFragment.class, b);
    mTabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab_selector);
    mTabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tab_selector);
    mTabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.tab_selector);

    mTabHost.getTabWidget().setShowDividers(TabWidget.SHOW_DIVIDER_MIDDLE);
    mTabHost.getTabWidget().setDividerDrawable(R.color.white);

tab_selector xml -

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/brown" android:state_selected="true"/>
    <item android:drawable="@color/black" android:state_selected="false"/>
    <item android:drawable="@color/red"/>

</selector>

布局xml -

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />

    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:foreground="@color/final_red"
        android:textAlignment="center"
        android:layout_gravity="center_vertical"

        android:foregroundGravity="center" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="50dp"
            android:layout_height="0dp"
            android:layout_weight="0" />
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

我使用过setDividerDrawable但是在执行代码后我看不到分隔符。 另外我想在标签之间显示分隔符。我能怎么做。 在附图中我想要分类,活动和图表之间的分隔。 文字应该居中对齐

enter image description here

1 个答案:

答案 0 :(得分:0)

这是我如何TabView使用FragmentTabHostTabWidget。试试这个:

<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#ff0000"
                android:gravity="center"
                android:orientation="horizontal"
                android:scrollbars="none"
                android:showDividers="middle"
                android:soundEffectsEnabled="true"
                android:splitMotionEvents="true"
                android:tabStripEnabled="true"
                android:textAlignment="inherit"
                android:textDirection="inherit" />
        </HorizontalScrollView>

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

</android.support.v4.app.FragmentTabHost>

修改

private FragmentTabHost mTabHost;
DatabaseHandler db = new DatabaseHandler(this);
SharedPreferences preferences;
Editor editor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    preferences = getApplicationContext().getSharedPreferences("My Loging",
            Context.MODE_PRIVATE);

    editor = preferences.edit();

    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);

    mTabHost.setup(this, getSupportFragmentManager(),
            android.R.id.tabcontent);
    mTabHost.addTab(
            mTabHost.newTabSpec("Home").setIndicator("Home",
                    getResources().getDrawable(R.drawable.home_icon)),
            HomeFragment.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("Product").setIndicator("Product",
                    getResources().getDrawable(R.drawable.home_icon)),
            ProductFragment.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("MyOrder").setIndicator("MyOrder",
                    getResources().getDrawable(R.drawable.my_order_icon)),
            MyOrderFragment.class, null);

    mTabHost.addTab(
            mTabHost.newTabSpec("AboutUs").setIndicator("About Us",
                    getResources().getDrawable(R.drawable.history_icon)),
            AboutUsFragment.class, null);

    mTabHost.addTab(
            mTabHost.newTabSpec("ContactUs").setIndicator("Contact Us",
                    getResources().getDrawable(R.drawable.history_icon)),
            ContactUsFragment.class, null);

}