【发布时间】:2014-12-22 10:43:13
【问题描述】:
如何在 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,但执行代码后看不到分隔符。 我也想在标签之间显示分隔符。我能怎么做。 在附图中,我想要类别、活动和图表之间的分隔符。 并且文本应该居中对齐
【问题讨论】:
-
在setdividerdrawble前加1行 tabhost.getTabWidget().setShowDividers(TabWidget.SHOW_DIVIDER_MIDDLE);
-
你检查了吗?>
-
@DigveshPatel 检查我更新的帖子和 logcat 错误
-
在 TabWidget "android:divider="@android:color:white" 中添加这一行
-
@yuva check this pastie.org/9793998 你刚刚换行了
标签: android fragment android-tabhost android-fragmentactivity