自定义底部tab

时间:2021-11-15 14:28:40
   public class MainActivity extends TabActivity implements
OnCheckedChangeListener {
private RadioGroup mainTab;
private TabHost tabhost;
private Intent iHome;
private Intent iNews;
private Intent iInfo;
private RadioButton radio_button1,radio_button0,radio_button2;
private String value=null; @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
value=getIntent().getStringExtra("value");
mainTab = (RadioGroup) findViewById(R.id.main_tab);
radio_button1=(RadioButton)findViewById(R.id.radio_button1);
radio_button0=(RadioButton)findViewById(R.id.radio_button0);
radio_button2=(RadioButton)findViewById(R.id.radio_button2);
mainTab.setOnCheckedChangeListener(this);
tabhost = getTabHost();
init(); } @SuppressLint("NewApi")
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radio_button0: //账单
tabhost.setCurrentTab(0);
Drawable drawable1=this.getResources().getDrawable(R.drawable.oneo);
radio_button0.setCompoundDrawablesRelativeWithIntrinsicBounds(null,drawable1,null,null);
Drawable drawable11=this.getResources().getDrawable(R.drawable.aaa);
radio_button1.setCompoundDrawablesRelativeWithIntrinsicBounds(null,drawable11,null,null);
Drawable drawable111=this.getResources().getDrawable(R.drawable.cccc);
radio_button2.setCompoundDrawablesRelativeWithIntrinsicBounds(null,drawable111,null,null);
break;
case R.id.radio_button1://信息
tabhost.setCurrentTab(1);
Drawable drawable0=this.getResources().getDrawable(R.drawable.two);
radio_button1.setCompoundDrawablesRelativeWithIntrinsicBounds(null,drawable0,null,null);
Drawable drawable00=this.getResources().getDrawable(R.drawable.bbb);
radio_button0.setCompoundDrawablesRelativeWithIntrinsicBounds(null,drawable00,null,null); Drawable drawable000=this.getResources().getDrawable(R.drawable.cccc);
radio_button2.setCompoundDrawablesRelativeWithIntrinsicBounds(null,drawable000,null,null); break;
case R.id.radio_button2://彩神岛
tabhost.setCurrentTab(2);
Drawable drawable2=this.getResources().getDrawable(R.drawable.threet);
radio_button2.setCompoundDrawablesRelativeWithIntrinsicBounds(null,drawable2,null,null);
Drawable drawable22=this.getResources().getDrawable(R.drawable.aaa);
radio_button1.setCompoundDrawablesRelativeWithIntrinsicBounds(null,drawable22,null,null);
Drawable drawable333=this.getResources().getDrawable(R.drawable.bbb);
radio_button0.setCompoundDrawablesRelativeWithIntrinsicBounds(null,drawable333,null,null); break; } } @SuppressLint("NewApi")
public void init() {
//初始化点击图标,跳转的接口
iNews = new Intent(this, BillActivity.class);
iNews.putExtra("value", "1");
tabhost.addTab(tabhost
.newTabSpec("iNews")
.setIndicator(getResources().getString(R.string.main_bill),
getResources().getDrawable(R.drawable.bbb))
.setContent(iNews));
iHome = new Intent(this, MessageActivity.class);
iHome.putExtra("value", "1");
tabhost.addTab(tabhost
.newTabSpec("iHome")
.setIndicator(getResources().getString(R.string.main_msg),
getResources().getDrawable(R.drawable.aaa))
.setContent(iHome));
iInfo = new Intent(this, SettingActivity.class);
iInfo.putExtra("value", "1");
tabhost.addTab(tabhost
.newTabSpec("iInfo")
.setIndicator(getResources().getString(R.string.main_setting),
getResources().getDrawable(R.drawable.cccc))
.setContent(iInfo));
Log.v("tag", "value:"+value);
if(value.equals("")||value.equals(null)||value.equals("1")){
tabhost.setCurrentTab(0);
} if(value.equals("2set")){
tabhost.setCurrentTab(2);
Drawable drawable2=this.getResources().getDrawable(R.drawable.threet);
radio_button2.setCompoundDrawablesRelativeWithIntrinsicBounds(null,drawable2,null,null);
Drawable drawable22=this.getResources().getDrawable(R.drawable.aaa);
radio_button1.setCompoundDrawablesRelativeWithIntrinsicBounds(null,drawable22,null,null);
Drawable drawable333=this.getResources().getDrawable(R.drawable.bbb);
radio_button0.setCompoundDrawablesRelativeWithIntrinsicBounds(null,drawable333,null,null);
}if(value.equals("2msg")){
tabhost.setCurrentTab(1);
Drawable drawable0=this.getResources().getDrawable(R.drawable.two);
radio_button1.setCompoundDrawablesRelativeWithIntrinsicBounds(null,drawable0,null,null);
Drawable drawable00=this.getResources().getDrawable(R.drawable.bbb);
radio_button0.setCompoundDrawablesRelativeWithIntrinsicBounds(null,drawable00,null,null);
Drawable drawable000=this.getResources().getDrawable(R.drawable.cccc);
radio_button2.setCompoundDrawablesRelativeWithIntrinsicBounds(null,drawable000,null,null); }
}

  xml中:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0" /> <TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.0"
android:visibility="gone" /> <TextView
android:id="@+id/line"
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="@color/black" /> <RadioGroup
android:id="@+id/main_tab"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/white"
android:gravity="center_vertical"
android:orientation="horizontal" > <RadioButton
android:id="@+id/radio_button0"
style="@style/main_tab_bottom"
android:layout_marginTop="0.0dip"
android:drawableTop="@drawable/oneo"
android:text="@string/main_bill" /> <RadioButton
android:id="@+id/radio_button1"
style="@style/main_tab_bottom"
android:layout_marginTop="0.0dip"
android:drawableTop="@drawable/aaa"
android:text="@string/main_msg" /> <RadioButton
android:id="@+id/radio_button2"
style="@style/main_tab_bottom"
android:layout_marginTop="0.0dip"
android:drawableTop="@drawable/cccc"
android:text="@string/main_setting" />
</RadioGroup>
</LinearLayout> </TabHost>

xml中的style:

<style name="main_tab_bottom">
<item name="android:textSize">@dimen/bottom_tab_font_size</item>
<item name="android:textColor">@color/black</item>
<item name="android:ellipsize">marquee</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:background">@color/white</item>
<item name="android:paddingTop">@dimen/bottom_tab_padding_up</item>
<item name="android:paddingBottom">@dimen/bottom_tab_padding_up</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:button">@null</item>
<item name="android:singleLine">true</item>
<item name="android:drawablePadding">@dimen/bottom_tab_padding_drawable</item>
<item name="android:layout_weight">1.0</item>
</style>

效果如下:

自定义底部tab