Mud*_*san 16 android android-widget
我有一个像这样的TabHost:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabhost"
android:background="@drawable/tabs_bg">
<LinearLayout
android:id="@+id/LinearLayout01"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginBottom="5dip">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</FrameLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我正在以编程方式向此TabHost添加标签,如下所示:
tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.setOnTabChangedListener(this);
/* tid1 is firstTabSpec Id. Its used to access outside. */
TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
TabSpec secondTabSpec = tabHost.newTabSpec("tid2");
TabSpec ThirdTabSpec = tabHost.newTabSpec("tid3");
/* TabSpec setIndicator() is used to set name for the tab. */
/* TabSpec setContent() is used to set content for a particular tab. */
firstTabSpec.setIndicator("Tab1", getResources().getDrawable(R.drawable.tab1));
secondTabSpec.setIndicator("Tab2", getResources().getDrawable(R.drawable.tab2));
ThirdTabSpec.setIndicator("Tab3", getResources().getDrawable(R.drawable.tab3));
firstTabSpec.setContent(new Intent(this,FirstTab.class));
secondTabSpec.setContent(new Intent(this,SecondTab.class));
ThirdTabSpec.setContent(new Intent(this,ThirdTab.class));
/* Add tabSpec to the TabHost to display. */
tabHost.addTab(firstTabSpec);
tabHost.addTab(secondTabSpec);
tabHost.addTab(ThirdTabSpec);
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#121312"));
}
tabHost.getTabWidget().setCurrentTab(0);
tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#f1a026"));
Run Code Online (Sandbox Code Playgroud)
这是onTabChanged事件:
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#121312"));
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#f1a026"));
}
Run Code Online (Sandbox Code Playgroud)
在onTabChanged事件中,我还想更改所有选项卡的文本颜色.请帮助我如何在活动中更改标签的文字颜色?
谢谢,
Far*_*han 52
要更改选项卡的文本颜色,您需要获取视图,即设置为选项卡标题的TextView,您可以像这样更改它:
TabHost tabhost = getTabHost();
for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
{
TextView tv = (TextView) tabhost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(.....);
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助....
Ans*_*oid 26
对于新设计支持选项卡布局; 你可以在你的xml中定义它
app:tabTextColor="@color/tab_inactive" app:tabSelectedTextColor="@color/tab_active"
-
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabanim_tabs"
app:tabTextColor="@color/tab_inactive"
app:tabSelectedTextColor="@color/tab_active"
android:textAllCaps="false"
/>
Run Code Online (Sandbox Code Playgroud)
以编程方式,它可能是这样的:
tabLayout.setTabTextColors(ContextCompat.getColorStateList(this, R.color.tab_selector));
tabLayout.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.indicator));
Run Code Online (Sandbox Code Playgroud)
spe*_*pes 11
对我来说@Farhan的解决方案没有用,因为在有四个标签的情况下getChildCount()继续返回1.使用getTabCount()并getChildTabViewAt()为我解决:
for (int tabIndex = 0 ; tabIndex < mTabHost.getTabWidget().getTabCount() ; tabIndex ++) {
View tab = mTabHost.getTabWidget().getChildTabViewAt(tabIndex);
TextView t = (TextView)tab.findViewById(android.R.id.title);
t.setTextColor(getResources().getColor(R.color.white));
}
Run Code Online (Sandbox Code Playgroud)
我想我会为有同样问题的人发布这个替代方案.
| 归档时间: |
|
| 查看次数: |
45430 次 |
| 最近记录: |