android - 选项卡切换时怎么改变布局中的内容
天蓬老师
天蓬老师 2017-04-17 11:09:36
[Android讨论组]

设计了这样一个选项卡界面

<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中怎么初始化这个视图呢?
谢谢!

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(3)
怪我咯

有好多的方式,由简开始:
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事件

ringa_lee

最简单直接的办法当然是切换不同的activity啦,和web 前段的tab菜单切换其实思路是有点类似的......

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号