android底部导航(带小红点提示) 导航布局TabHostAndroid底部角标 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 操蛋的CSDN竟然给出“创建失败文章内容 必须在10到10000字之内”的提示,死活发不了全文续接上文MMTabButton样式<style name="MMTabButton"> <item name="android:textSize">12.0dip</item> <item name="android:textColor">@color/main_tab_textcolor</item> <item name="android:gravity">center_horizontal</item> <item name="android:background">@null</item> <item name="android:layout_width">0.0dip</item> <item name="android:layout_height">wrap_content</item> <item name="android:layout_marginLeft">0.70000005dip</item> <item name="android:layout_marginRight">0.70000005dip</item> <item name="android:button">@null</item> <item name="android:layout_weight">1.0</item> </style>其中main_tab_textcolor和main_tab_selector1/2/3/4定义的都为selector,目的是为了实现点击改变图标和文字颜色<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:color="@color/main_color"/> <item android:state_checked="true" android:color="@color/main_color"/> <item android:color="@color/bottom_navigation_bar_normal_textcolor"/></selector><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/main_tab_selector1_selected" android:state_checked="true"/> <item android:drawable="@drawable/main_tab_selector1_normal"/></selector>由于布局文件中没有使用RadioGroup包着四个RadioButton,并且其中的RadioButton之间又掺杂着其他布局。所以简单的通过使用RadioGroup包着四个RadioButton是满足不了需求的,只能在代码中控制点击操作,即通过switchState方法模拟RadioGroup只能单选的事件。 Java代码的实现:import android.app.TabActivity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.widget.CompoundButton;import android.widget.RadioButton;import android.widget.TabHost;import android.widget.CompoundButton.OnCheckedChangeListener;public class MainActivity extends TabActivity implements OnCheckedChangeListener { private TabHost tabHost; private Intent firstIntent; private Intent secondIntent; private Intent thirdIntent; private Intent fourthIntent; private RadioButton firstBt; private RadioButton secondBt; private RadioButton thirdBt; private RadioButton fourthBt; private int mState = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); firstIntent = new Intent(this, ActionItems.class); secondIntent = new Intent(this, ActionItems.class); thirdIntent = new Intent(this, ActionItems.class); fourthIntent = new Intent(this, ActionItems.class); firstBt = (RadioButton) findViewById(R.id.main_tab_first); secondBt = (RadioButton) findViewById(R.id.main_tab_second); thirdBt = (RadioButton) findViewById(R.id.main_tab_three); fourthBt = (RadioButton) findViewById(R.id.main_tab_four); tabHost = getTabHost(); tabHost.addTab(tabHost.newTabSpec("first").setIndicator("first").setContent(firstIntent)); tabHost.addTab(tabHost.newTabSpec("second").setIndicator("second").setContent(secondIntent)); tabHost.addTab(tabHost.newTabSpec("third").setIndicator("third").setContent(thirdIntent)); tabHost.addTab(tabHost.newTabSpec("fourth").setIndicator("fourth").setContent(fourthIntent)); firstBt.setOnCheckedChangeListener(this); secondBt.setOnCheckedChangeListener(this); thirdBt.setOnCheckedChangeListener(this); fourthBt.setOnCheckedChangeListener(this); } public void setCurrentTab(int index) { switchState(index); } private void switchState(int state) { if (mState == state) { return; } // else continue mState = state; firstBt.setChecked(false); secondBt.setChecked(false); thirdBt.setChecked(false); fourthBt.setChecked(false); switch (mState) { case 0: firstBt.setChecked(true); tabHost.setCurrentTabByTag("first"); break; case 1: secondBt.setChecked(true); tabHost.setCurrentTabByTag("second"); break; case 2: thirdBt.setChecked(true); tabHost.setCurrentTabByTag("third"); break; case 3: fourthBt.setChecked(true); tabHost.setCurrentTabByTag("fourth"); break; default: break; } } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if (isChecked) { switch (buttonView.getId()) { case R.id.main_tab_first: switchState(0); break; case R.id.main_tab_second: switchState(1); break; case R.id.main_tab_three: switchState(2); break; case R.id.main_tab_four: switchState(3); break; default: break; } } }} 能否直接给个link呢。。我们去下 Android代码解释,求C++牛人赐教 RadioButton 的问题 android如何使用syncML来同步通讯录 询问一个关于空指针的问题和AVD模拟器横竖屏的问题 android中如何实现图片的水平翻转 Android开发做哪方面好? android 游戏开发 如何实现按menu键,弹出顶部+底部菜单(类似于qq阅读器菜单效果) 如何得知系统进入深度休眠或者从深度休眠中被唤醒? android小白setonclicklistener哪里错了 Android开发 天气预报信息获得 andriod串口接收
<style name="MMTabButton">
<item name="android:textSize">12.0dip</item>
<item name="android:textColor">@color/main_tab_textcolor</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:background">@null</item>
<item name="android:layout_width">0.0dip</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginLeft">0.70000005dip</item>
<item name="android:layout_marginRight">0.70000005dip</item>
<item name="android:button">@null</item>
<item name="android:layout_weight">1.0</item>
</style>
其中main_tab_textcolor和main_tab_selector1/2/3/4定义的都为selector,目的是为了实现点击改变图标和文字颜色
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/main_color"/>
<item android:state_checked="true" android:color="@color/main_color"/>
<item android:color="@color/bottom_navigation_bar_normal_textcolor"/>
</selector>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/main_tab_selector1_selected" android:state_checked="true"/>
<item android:drawable="@drawable/main_tab_selector1_normal"/>
</selector>由于布局文件中没有使用RadioGroup包着四个RadioButton,并且其中的RadioButton之间又掺杂着其他布局。所以简单的通过使用RadioGroup包着四个RadioButton是满足不了需求的,只能在代码中控制点击操作,即通过switchState方法模拟RadioGroup只能单选的事件。
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TabHost;
import android.widget.CompoundButton.OnCheckedChangeListener;public class MainActivity extends TabActivity implements OnCheckedChangeListener {
private TabHost tabHost;
private Intent firstIntent;
private Intent secondIntent;
private Intent thirdIntent;
private Intent fourthIntent;
private RadioButton firstBt;
private RadioButton secondBt;
private RadioButton thirdBt;
private RadioButton fourthBt; private int mState = 0; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstIntent = new Intent(this, ActionItems.class);
secondIntent = new Intent(this, ActionItems.class);
thirdIntent = new Intent(this, ActionItems.class);
fourthIntent = new Intent(this, ActionItems.class);
firstBt = (RadioButton) findViewById(R.id.main_tab_first);
secondBt = (RadioButton) findViewById(R.id.main_tab_second);
thirdBt = (RadioButton) findViewById(R.id.main_tab_three);
fourthBt = (RadioButton) findViewById(R.id.main_tab_four);
tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("first").setIndicator("first").setContent(firstIntent));
tabHost.addTab(tabHost.newTabSpec("second").setIndicator("second").setContent(secondIntent));
tabHost.addTab(tabHost.newTabSpec("third").setIndicator("third").setContent(thirdIntent));
tabHost.addTab(tabHost.newTabSpec("fourth").setIndicator("fourth").setContent(fourthIntent));
firstBt.setOnCheckedChangeListener(this);
secondBt.setOnCheckedChangeListener(this);
thirdBt.setOnCheckedChangeListener(this);
fourthBt.setOnCheckedChangeListener(this);
}
public void setCurrentTab(int index) {
switchState(index);
} private void switchState(int state) {
if (mState == state) {
return;
} // else continue mState = state;
firstBt.setChecked(false);
secondBt.setChecked(false);
thirdBt.setChecked(false);
fourthBt.setChecked(false); switch (mState) {
case 0:
firstBt.setChecked(true);
tabHost.setCurrentTabByTag("first");
break; case 1:
secondBt.setChecked(true);
tabHost.setCurrentTabByTag("second");
break; case 2:
thirdBt.setChecked(true);
tabHost.setCurrentTabByTag("third");
break; case 3:
fourthBt.setChecked(true);
tabHost.setCurrentTabByTag("fourth");
break; default:
break;
}
} @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
switch (buttonView.getId()) {
case R.id.main_tab_first:
switchState(0);
break;
case R.id.main_tab_second:
switchState(1);
break;
case R.id.main_tab_three:
switchState(2);
break;
case R.id.main_tab_four:
switchState(3);
break; default:
break;
}
}
}}