[英]Android - Change background color of Tab onClicked
I have designed an app in that I am using TabBar
and I want to change the BackgroundColor of TabBar
when selected or pressed.. In the class where I am implementing TabBar
is extending Fragment
not TabActivity
. 我设计的,我使用的应用程序TabBar
和我想改变的BACKGROUNDCOLOR TabBar
选择或按下时。在这里我实现了类TabBar
是延长Fragment
不TabActivity
。 I have implemented following 我实现了以下
tabHost = (TabHost) v.findViewById(R.id.tabhost);
mLocalActivityManager = new LocalActivityManager(getActivity(), false);
Intent intentongoing = new Intent().setClass(getActivity(), Properties_org_screen.class);
TabSpec tabSpecOngoing = tabHost.newTabSpec("On Going").setIndicator("On Going").setContent(intentongoing);
Intent intentcomplete = new Intent().setClass(getActivity(), Properties_org_screen.class);
TabSpec tabSpecComplete = tabHost.newTabSpec("Completed").setIndicator("Completed").setContent(intentcomplete);
Intent intentproposed = new Intent().setClass(getActivity(), Properties_org_screen.class);
TabSpec tabSpecProposed = tabHost.newTabSpec("Proposed").setIndicator("Proposed").setContent(intentproposed);
mLocalActivityManager.dispatchCreate(savedInstanceState);
tabHost.setup(mLocalActivityManager);
tabHost.addTab(tabSpecOngoing);
tabHost.addTab(tabSpecComplete);
tabHost.addTab(tabSpecProposed);
tabHost.setCurrentTab(0);
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++)
{
tabHost.getTabWidget().getChildAt(i).setPadding(2,5,0,10);
Log.v("","In for loop");
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
tv.setTextColor(Color.WHITE);
}
**OnCreate Ends**
@Override
public void onTabChanged(String arg0)
{
// TODO Auto-generated method stub
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(getResources().getColor(R.color.blue_title));
}
}
I also tried to implement Custom_tab.xml
as, 我还尝试将Custom_tab.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/blue"/>
<item android:state_focused="true" android:drawable="@drawable/new_background"/>
<item android:state_pressed="true" android:drawable="@drawable/blue"/>
</selector>
and calling this file in 并在此文件中调用
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent" android:layout_height="40dp"
android:layout_gravity="center" android:gravity="center" android:background="@drawable/custom_tab"/>
Still not working.. What I am missing?? 仍然无法正常工作。
Use this in OnCreate
在OnCreate
使用它
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++)
{
tabHost.getTabWidget().getChildAt(i).setPadding(2,5,0,10);
Log.v("","In for loop");
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title); //Unselected Tabs
tv.setTextColor(Color.WHITE);
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.new_background);
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(getResources().getColor(R.color.blue_title));
and TabChanged
method 和TabChanged
方法
@Override
public void onTabChanged(String arg0) {
// TODO Auto-generated method stub
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.new_background);
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(getResources().getColor(R.color.blue_title));
}
It will display the blue color to tab which is selected and a white image named new_background to the tab which is not selected.. 它将在选择的选项卡上显示蓝色,而在未选择的选项卡上显示名为new_background的白色图像。
and don't forget to write 而且不要忘了写
implements OnTabChangeListener
after your class name 在你的班级名字之后
and 和
tabHost.setOnTabChangedListener(this);
after 后
tabHost = (TabHost) v.findViewById(R.id.tabhost);
This one is the Perfect Solution I guess. 我猜这是一个完美的解决方案 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.