目前我正在尝试为我的项目实现FragmentTabHost.我仍然对这些碎片很新,但我发现它在重用布局等方面非常好,这就是为什么我想进一步推动它.现在我阅读有关如何使用片段创建Tabs的教程,我在本教程中介绍了:
http://maxalley.wordpress.com/2013/05/18/android-creating-a-tab-layout-with-fragmenttabhost-and-fragments/
现在这个工作得很好,除了tabWidget位于我的布局之上,我希望它在底部.我发现在初始化所有选项卡后我需要设置tabWidget,所以我尝试添加这些代码:
mTabWidget = (TabWidget) findViewById(android.R.id.tabs);
mTabWidget.setBackgroundColor(Color.WHITE);
mTabWidget.setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
mTabWidget.setGravity(Gravity.BottOM);
现在这个已经消除了分隔符并改变了颜色,但显然不会将我的Widget放在我的布局的底部.现在我该怎么做?
我还尝试编辑Tabhost xml并将TabWidget放在FrameLayout之后但没有任何反应.这是xml:
<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" >
<FrameLayout
android:id="@+id/tabFrameLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal"
/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
解决方法
我已经参考了这个链接
github example
这将是你的布局:
<?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" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
对于您的自定义标签:
mTabHost.addTab(setIndicator(mTabHost.newTabSpec("Tab1"),R.drawable.image1),public TabSpec setIndicator(Context ctx,TabSpec spec,int resid) {
// Todo Auto-generated method stub
View v = LayoutInflater.from(ctx).inflate(R.layout.tabs_text,null);
v.setBackgroundResource(resid);
TextView text = (TextView) v.findViewById(R.id.tab_title);
text.setText(spec.getTag());
return spec.setIndicator(v);
}
编辑
//To set drawable to your perticular TAB mTabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab_login);
结束编辑
如果要添加drawable(选择器):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/tab_compose_h" android:state_selected="true"/>
<item android:drawable="@drawable/tab_compose_h" android:state_pressed="true"/>
<item android:drawable="@drawable/tab_compose"/>
</selector>