}
- 第三步,通过设置 监听器来实现底部tab颜色和图案样式的转换
mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
int currentTab = mTabHost.getCurrentTab();
Log.i(TAG, “onTabChanged:currentTab:=” + currentTab);
updateTab(currentTab);
}
});
运行上述代码及可以看到如下效果图
使用RadioGroup和RadioButton实现仿qq底部切换
第一步 ,先看布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout
android:id="@+id/activity_second_style"
xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”
<FrameLayout
android:id="@+id/fl"
android:layout_width=“match_parent”
android:layout_height=“0dp”
android:layout_weight=“1”>
<RadioGroup
android:id="@+id/rg"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:orientation=“horizontal”
<RadioButton
android:id="@+id/rb_home"
style="@style/bottom_tab"
android:drawableTop="@drawable/sel_home"
android:text=“首页”/>
<RadioButton
android:id="@+id/rb_course"
style="@style/bottom_tab"
android:drawableTop="@drawable/sel_course"
android:text=“课程”/>
<RadioButton
android:id="@+id/rb_direct_seeding"
style="@style/bottom_tab"
android:drawableTop="@drawable/sel_direct_seeding"
android:text=“直播”/>
<RadioButton
android:id="@+id/rb_me"
style="@style/bottom_tab"
android:drawableTop="@drawable/sel_me"
android:text=“我的”/>
其实每一个tab的选中时利用RadioGroup中RadioButton的互相排斥的特性,即每一次只能选中一个 RadioButton
至于bottom_tab的style,只不过是将相同的arr提取出来,减少布局的代码量和方便统一修改而已,平时我们在写布局代码 的时候也可以这样
第二步,我们来看一下Activity的 代码
public class ThreeActivity extends AppCompatActivity {
FrameLayout mFl;
RadioGroup mRg;
private FragmentManager mFragmentManager;
private int position = 0;
public static final String[] mTiltles = new String[]{
“首页”, “课程”, “直播”, “个人”
};
private List mFragments;
private Fragment mCurFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three);
mFl = (FrameLayout) findViewById(R.id.fl);
mRg = (RadioGroup) findViewById(R.id.rg);
mFragments = new ArrayList<>();
for (int i = 0; i < mTiltles.length; i++) {