设计了这样一个选项卡界面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
</LinearLayout>
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@drawable/maintab_toolbar_bg"
android:gravity="center"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@drawable/home_btn_bg"
android:button="@null"
android:drawableTop="@drawable/icon_1_n"
android:gravity="center"
android:paddingTop="5dp"
android:text="首页"
android:textSize="12sp" />
<RadioButton
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/home_btn_bg"
android:button="@null"
android:drawableTop="@drawable/icon_2_n"
android:gravity="center"
android:paddingTop="5dp"
android:text="短信"
android:textSize="12sp" />
<RadioButton
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/home_btn_bg"
android:button="@null"
android:drawableTop="@drawable/icon_3_n"
android:gravity="center"
android:paddingTop="5dp"
android:text="联系人"
android:textSize="12sp" />
<RadioButton
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/home_btn_bg"
android:button="@null"
android:drawableTop="@drawable/icon_4_n"
android:gravity="center"
android:paddingTop="5dp"
android:text="搜索"
android:textSize="12sp" />
</RadioGroup>
</LinearLayout>
这个布局中有一个线性布局,我该怎么样实现单击不同的选项卡改变这个线性布局中的内容呢?
在Main.java中怎么初始化这个视图呢?
谢谢!
有好多的方式,由简开始:
1. linearlayout是一个容器,那么你可以用LayoutInflater 去获取一个子的view,然后加入这个layout里面,然后点击别的button,就remove掉之前的view,然后再inflate一个新的子view,加入这个layout里面
Demo
LinearLayout container;
button1.setOnClickListener(new View.OnClickListener() {
void onClick(View v) {
container.removeAllViews();
container.addView(inflater.inflate(R.layout.tab1, null));
}
});
button2.setOnClickListener(new View.OnClickListener() {
void onClick(View v) {
container.removeAllViews();
container.addView(inflater.inflate(R.layout.tab2, null));
}
});
代码比较丑,思路就是这样的
2. 使用viewPager,viewSwitch,等多个容器的组件去装子view
ViewPager文档
ViewPager例子
ViewSwitcher文档
ViewSwitch例子
3. 使用fragment去管理这个容器
Fragment文档
Fragment例子
难易度上,3>2>1
看自己需求了。
虽然我没有搞过android开发,但我觉得用户切换的时候应该会触发RadioGroup的一个类似actived事件
最简单直接的办法当然是切换不同的activity啦,和web 前段的tab菜单切换其实思路是有点类似的……
正文完