我正在尝试使用android.support.v4.app.FragmentTabHost创建标签。
我试图将标签的背景颜色更改为白色,将标签的文字颜色更改为黑色,但它不起作用。
以下是我的代码。
<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">
<TabWidget
android:id="@android:id/tabs"
android:textColor="@color/trans_white"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
答案 0 :(得分:1)
尝试这种方式来自定义您的tabhost
tab_selector.xml
<?xml version="1.0" encoding="utf8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<! When selected, use grey >
<item android:drawable="@drawable/tabselectedcolor" android:state_selected="true"/>
<! When not selected, use white >
<item android:drawable="@drawable/tabunselcolor"/>
</selector>
XML
<?xml version="1.0" encoding="utf8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/host"
android:layout_width="fillparent"
android:layout_height="fillparent" >
<RelativeLayout
android:layout_width="fillparent"
android:layout_height="fillparent"
android:orientation="vertical"
android:padding="6dp" >
<TabWidget
android:id="@android:id/tab"
android:layout_width="fillparent"
android:layout_height="wrapcontent" />
<FrameLayout
android:layout_below="@android:id/tab"
android:id="@android:id/tabattributes"
android:layout_width="fillparent"
android:layout_height="fillparent"
android:padding="6dp" />
</RelativeLayout>
</TabHost>
Java代码
public class TabCustomizationActivity extends TabActivity implements
OnTabChangeListener {
/** Called when the activity is first created. */
TabHost tHost;
@Override
public void onCreate(savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources resources = getResources();
tHost = getTabHost();
TabHost.TabSpec tSpec;
Intent intent;
intent = new Intent().setClass(this, FirstActivity.class);
tSpec = tHost.newTabSpec("first").setIndicator("One")
.setContent(intent);
tHost.addTab(tSpec);
intent = new Intent().setClass(this, FirstActivity.class);
tSpec = tHost.newTabSpec("second").setIndicator("Second")
.setContent(intent);
tHost.addTab(tSpec);
intent = new Intent().setClass(this, FirstActivity.class);
tSpec = tHost.newTabSpec("third").setIndicator("Third")
.setContent(intent);
tHost.addTab(tSpec);
tHost.setCurrentTab(0); // Default Selected Tab
tHost.setOnTabChangedListener(this);
tHost.getTabWidget().getChildAt(0).getLayoutParams().height = 40;
tHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.WHITE);
tHost.getTabWidget().getChildAt(1).getLayoutParams().height = 40;
tHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.WHITE);
tHost.getTabWidget().getChildAt(2).getLayoutParams().height = 40;
tHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.WHITE);
tHost.getTabWidget().getChildAt(0)
.setBackgroundColor(Color.rgb(00, 219, 239));
}
@Override
public void onTab(String tabId) {
if (tabId.equals("first")) {
tHost.getTabWidget().getChildAt(0)
.setBackgroundResource(R.drawable.tab_selector);
tHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.WHITE);
tHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.WHITE);
} else if (tabId.equals("second")) {
tHost.getTabWidget().getChildAt(1)
.setBackgroundResource(R.drawable.tab_selector);
tHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.WHITE);
tHost.getTabWidget().getChildAt(2).setBackgroundColor(Color.WHITE);
} else if (tabId.equals("third")) {
tHost.getTabWidget().getChildAt(2)
.setBackgroundResource(R.drawable.tab_selector);
tHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.WHITE);
tHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.WHITE);
}
}
}
有关详情,请参阅this
答案 1 :(得分:0)
现在你不应该使用TabHost来创建带Tab的滑动页面。获得它的更好方法是:
对于带有标签的滑动页面,请执行以下操作。
在github上下载或复制以下两个文件并粘贴您的项目。这与developers.google.com相同,但setDistributeEvenly方法除外。
activity_main.xml中
<your.package.name.SlidingTabLayout
android:clickable="true"
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</your.package.name.SlidingTabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
MyAdapter.java(这里我只用了两页)
class MyPagerAdapter extends FragmentPagerAdapter
{
String[] title = {"All","Favourites"};
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment=null;
if (position==0)
fragment= new All();
if (position==1)
fragment= new Favourites();
return fragment;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
return title[position];
}
}
tab_view.xml(仅限标签视图,如果您需要,您也可以在此处使用ImageView)
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/tab_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text=""
android:padding="15dp"
android:textStyle="bold"
android:textSize="25dp"
/>
</FrameLayout>
MainActivity.java
private SlidingTabLayout tabLayout;
private ViewPager pager;
tabLayout= (SlidingTabLayout) findViewById(R.id.tabs);
pager = (ViewPager) findViewById(R.id.pager);
tabLayout.setCustomTabView(R.layout.tab_view,R.id.tab_title);
MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
tabLayout.setDistributeEvenly(true);
tabLayout.setViewPager(pager);