Java 自定义选项卡主机和选项卡(带片段)
我正在构建一个包含tabhost的应用程序,它运行良好。我的问题是,我不知道如何自定义tabhost(背景色、选中和未选中的项目…)。我的代码是: MainActivity.javaJava 自定义选项卡主机和选项卡(带片段),java,android,android-fragments,android-tabhost,Java,Android,Android Fragments,Android Tabhost,我正在构建一个包含tabhost的应用程序,它运行良好。我的问题是,我不知道如何自定义tabhost(背景色、选中和未选中的项目…)。我的代码是: MainActivity.java mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost); mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("Home").setIndicator("Home"),
HomeFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("que hacer").setIndicator("¿Qué hacer?"),
HacerFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("donde").setIndicator("¿A dónde ir?"),
DestacadoFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("mapa").setIndicator("Lee tu mapa"),
LectorFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("captura").setIndicator("Captura"),
CapturaFragment.class, null);
我的布局是这样的:
<android.support.v4.app.FragmentTabHost
android:id="@+id/tabhost_sup"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_weight="0"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
非常感谢;) 出于您的考虑,您需要实现如下选择器 您的选项卡选择器。。希望你能实现剩下的 选项卡\u选择\u learn.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/learn" />
<item android:drawable="@drawable/learng" />
</selector>
像这样使用
您的Home.java类
public class Home extends FragmentActivity {
private static final String TAB_1_TAG = "tab_1";
private static final String TAB_2_TAG = "tab_2";
private static final String TAB_3_TAG = "tab_3";
private static final String TAB_4_TAG = "tab_4";
private static final String TAB_5_TAG = "tab_5";
private FragmentTabHost mTabHost;
View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.home);
initView();
view = (View) findViewById(R.drawable.tab_indicator);
// setTabs();
}
private void initView() {
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(
mTabHost.newTabSpec(TAB_1_TAG).setIndicator("",
getResources().getDrawable(R.drawable.tab_select_talk)),
TalkContainerFragment.class, null);
// mTabHost.addTab(mTabHost.newTabSpec(TAB_1_TAG).setIndicator("Talk"),
// TalkContainerFragment.class, null);
mTabHost.addTab(
mTabHost.newTabSpec(TAB_2_TAG).setIndicator("",
getResources().getDrawable(R.drawable.tab_select_learn)),
LearnContainerFragment.class, null);
mTabHost.addTab(
mTabHost.newTabSpec(TAB_3_TAG).setIndicator("",
getResources().getDrawable(R.drawable.tab_select_go)),
GoContainerFragment.class, null);
mTabHost.addTab(
mTabHost.newTabSpec(TAB_4_TAG).setIndicator("",
getResources().getDrawable(R.drawable.tab_select_watch)),
WatchContainerFragment.class, null);
mTabHost.addTab(
mTabHost.newTabSpec(TAB_5_TAG).setIndicator("",
getResources().getDrawable(R.drawable.tab_select_more)),
MoreContainerFragment.class, null);
// Inflating color for the first time.
for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
mTabHost.getTabWidget().getChildAt(i)
.setBackgroundColor(Color.parseColor("#181818"));
}
mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab())
.setBackgroundColor(Color.parseColor("#424542"));
// ============== End of color inflation ==================
mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
// Inflating color when tab is selected.
for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
mTabHost.getTabWidget().getChildAt(i)
.setBackgroundColor(Color.parseColor("#181818"));
}
mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab())
.setBackgroundColor(Color.parseColor("#424542"));
// ============== End of color inflation ==================
}
});
for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
final TextView tv = (TextView) mTabHost.getTabWidget()
.getChildAt(i).findViewById(android.R.id.title);
// mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.talk);
if (tv == null)
continue;
else
tv.setTextSize(8);
}
}
@Override
public void onBackPressed() {
boolean isPopFragment = false;
String currentTabTag = mTabHost.getCurrentTabTag();
if (currentTabTag.equals(TAB_1_TAG)) {
isPopFragment = ((BaseContainerFragment) getSupportFragmentManager()
.findFragmentByTag(TAB_1_TAG)).popFragment();
} else if (currentTabTag.equals(TAB_2_TAG)) {
isPopFragment = ((BaseContainerFragment) getSupportFragmentManager()
.findFragmentByTag(TAB_2_TAG)).popFragment();
} else if (currentTabTag.equals(TAB_3_TAG)) {
isPopFragment = ((BaseContainerFragment) getSupportFragmentManager()
.findFragmentByTag(TAB_3_TAG)).popFragment();
} else if (currentTabTag.equals(TAB_4_TAG)) {
isPopFragment = ((BaseContainerFragment) getSupportFragmentManager()
.findFragmentByTag(TAB_4_TAG)).popFragment();
} else if (currentTabTag.equals(TAB_5_TAG)) {
isPopFragment = ((BaseContainerFragment) getSupportFragmentManager()
.findFragmentByTag(TAB_5_TAG)).popFragment();
}
if (!isPopFragment) {
finish();
}
}
}
公共类主页扩展了碎片活动{
私有静态最终字符串TAB_1_TAG=“TAB_1”;
私有静态最终字符串TAB_2_TAG=“TAB_2”;
私有静态最终字符串TAB_3_TAG=“TAB_3”;
私有静态最终字符串TAB_4_TAG=“TAB_4”;
私有静态最终字符串TAB_5_TAG=“TAB_5”;
私有碎片选项卡主机mTabHost;
视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(窗口。功能\u无\u标题);
setContentView(R.layout.home);
initView();
视图=(视图)findViewById(R.drawable.tab_指示器);
//setTabs();
}
私有void initView(){
mTabHost=(FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(这个,getSupportFragmentManager(),R.id.realtabcontent);
mTabHost.addTab(
mTabHost.newTabSpec(标签1)。setIndicator(“,
getResources().getDrawable(R.drawable.tab\u select\u talk)),
TalkContainerFragment.class,空);
//mTabHost.addTab(mTabHost.newTabSpec(TAB_1_标记).setIndicator(“Talk”),
//TalkContainerFragment.class,空);
mTabHost.addTab(
mTabHost.newTabSpec(TAB_2_TAG).setIndicator(“,
getResources().getDrawable(R.drawable.tab\u select\u learn)),
LearnContainerFragment.class,空);
mTabHost.addTab(
mTabHost.newTabSpec(标签3)。setIndicator(“,
getResources().getDrawable(R.drawable.tab\u select\u go)),
GoContainerFragment.class,null);
mTabHost.addTab(
mTabHost.newTabSpec(TAB_4_TAG).setIndicator(“,
getResources().getDrawable(R.drawable.tab\u select\u watch)),
WatchContainerFragment.class,null);
mTabHost.addTab(
mTabHost.newTabSpec(TAB_5_TAG).setIndicator(“,
getResources().getDrawable(R.drawable.tab\u选择\u更多)),
MoreContainerFragment.class,null);
//第一次膨胀颜色。
对于(int i=0;i
希望它能消除你的疑虑 谢谢你的回答。我可以用两个循环来改变背景色,但是tab_选择器不起作用。这是“view=(view)findViewById(R.drawable.tab_indicator);”的功能?使用选择器可以提供比使用循环设置背景图像更多的功能:先生,请回答这个问题@BishnuKumar请查看我对您的评论link@Y