Framelayout帧布局和ViewPager和Tablayout的连用

本文介绍了一种在Android应用中实现底部标签切换的方法,通过使用LinearLayout、FrameLayout和RadioGroup来布局界面,并结合Fragment进行内容展示。文章详细展示了如何在MainActivity中初始化布局,加载Fragment,并监听RadioGroup的切换事件,以动态显示和隐藏不同的Fragment。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"


    >
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="9"
    android:id="@+id/frame_layout"
    ></FrameLayout>
    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:id="@+id/radio_group"
        android:orientation="horizontal"
        >
        <RadioButton
            android:id="@+id/ra1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:button="@null"
            android:text="我的"
            android:gravity="center"
            android:drawableTop="@drawable/sel"
            android:checked="true"
            />
        <RadioButton
            android:id="@+id/ra2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:button="@null"
            android:text="你的"
            android:gravity="center"
            android:drawableTop="@drawable/sel"
            />
        <RadioButton
            android:id="@+id/ra3"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:button="@null"
            android:text="他的"
            android:gravity="center"
            android:drawableTop="@drawable/sel"
            />
    </RadioGroup>


</LinearLayout>

实现底部切换

public class MainActivity extends BaseActivity {


    private FrameLayout layout;
    private RadioGroup group;
    private com.example.framelayout_tablayout.fragment.frag1 frag1;
    private com.example.framelayout_tablayout.fragment.frag2 frag2;
    private com.example.framelayout_tablayout.fragment.frag3 frag3;
    private FragmentManager supportFragmentManager;

    @Override
    public int bindlayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        layout = findViewById(R.id.frame_layout);
        group = findViewById(R.id.radio_group);
    }

    @Override
    protected void intData() {
       //模拟数据
        frag1 = new frag1();
        frag2 = new frag2();
        frag3 = new frag3();
       //获取Fragmen管理器
        supportFragmentManager = getSupportFragmentManager();
        //开启
        FragmentTransaction transaction = supportFragmentManager.beginTransaction();
        //替换Fragment
        transaction.
                add(R.id.frame_layout, frag1)
                .add(R.id.frame_layout,frag2)
                .add(R.id.frame_layout,frag3)
                .show(frag1)
                .hide(frag2)
                .hide(frag3)
                .commit();



    }

    @Override
    protected void Event() {
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                FragmentTransaction transaction = supportFragmentManager.beginTransaction();
                switch (checkedId){
                    case R.id.ra1:
                        transaction.show(frag1)
                                    .hide(frag2)
                                    .hide(frag3);
                        break;
                    case R.id.ra2:
                        transaction.show(frag2)
                                .hide(frag3)
                                .hide(frag1);
                        break;
                    case R.id.ra3:
                        transaction.show(frag3)
                                .hide(frag2)
                                .hide(frag1);
                        break;
                }
                transaction.commit();
            }
        });

    }
}

//上面的布局

LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f0f"
    android:orientation="vertical"
    >
    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tab_layout"
        ></android.support.design.widget.TabLayout>
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/view_pager"
        ></android.support.v4.view.ViewPager>
</LinearLayout>
public class frag1 extends BaseFragment {

    private ViewPager viewPager;
    private TabLayout tabLayout;
    private ArrayList<Fragment> list;

    @Override
    public int bindlayout() {
        return R.layout.frag01;
    }

    @Override
    protected void initView() {
        viewPager = getView().findViewById(R.id.view_pager);
        tabLayout = getView().findViewById(R.id.tab_layout);
    }

    @Override
    protected void intData() {
        //创建导航
        //导航窗口
        String [] str={"首页","爱好","喜欢","收藏","添加","关注"};
        list = new ArrayList<>();
        list.add(new tab1());
        list.add(new tab2());
        list.add(new tab3());
        list.add(new tab4());
        list.add(new tab5());
        list.add(new tab6());

        //设置适配器
        viewPager.setAdapter(new VAdapter(getActivity().getSupportFragmentManager(),list,str));

        //绑定Tab
        tabLayout.setupWithViewPager(viewPager);
    }

    @Override
    protected void Event() {

    }
### 关于 FrameLayout 帧布局 #### 定义与初始化 `FrameLayout` 是一种特殊的布局管理器,在 Android 应用程序中用于创建重叠视图的效果。通过 `findViewById()` 方法可以获取到定义在 XML 文件中的 `FrameLayout` 对象实例[^1]。 ```java FrameLayout frame = (FrameLayout) findViewById(R.id.mylayout); ``` #### 主要属性说明 对于 `FrameLayout` 来说,有几个重要的属性可以帮助开发者更好地控制其外观: - **android:foreground** 设置该帧布局容器的前景图像。此功能允许在一个简单的背景上叠加复杂的图形元素,从而实现更丰富的视觉效果[^2]。 - **android:foregroundGravity** 控制上述提到的前景图片的具体位置。例如,当希望将前景放置于右下角时,则可指定如下参数值: ```xml android:foregroundGravity="right|bottom" ``` 值得注意的是,虽然有提及 `android:orientation` 其他一些通用属性,但实际上这些并不是专属于 `FrameLayout` 的特性;相反,它们更多见于像 `LinearLayout` 这样的线性排列型布局组件中[^3]。 #### 示例代码展示 下面给出一段完整的 Java 代码片段以及对应的 XML 配置来演示如何使用 `FrameLayout` 创建一个带有前景图标的界面部分: XML配置文件(activity_main.xml): ```xml <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mylayout" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- 背景 --> <ImageView android:src="@drawable/background_image" android:scaleType="centerCrop" android:layout_width="match_parent" android:layout_height="200dp"/> <!-- 前景图标 --> <ImageView android:src="@drawable/foreground_icon" android:contentDescription="@string/icon_description" android:layout_gravity="right|bottom" android:layout_marginBottom="8dp" android:layout_marginEnd="8dp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </FrameLayout> ``` Java端操作(MainActivity.java): ```java import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.FrameLayout; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FrameLayout frame = (FrameLayout)findViewById(R.id.mylayout); // 可在此处继续添加逻辑处理... } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值