Android基础(四)Dynamic Fragment

工具:Android Studio

目录

问题描述

代码

MainActivity.java

activity_main_xml

fragment_blank1

fragment_blank2

fragment_blank3

fragment_blank4

运行结果


问题描述

底部RadioGroup,上部Framelayout,有四个button,每个button对应一个页面,每个页面一个textview,内容为button的text内容即可。

【所有代码在项目DynamicFragment中】 

代码

MainActivity.java

代码:

package com.example.dynamicfragment;



import androidx.appcompat.app.AppCompatActivity;

import androidx.fragment.app.Fragment;

import androidx.fragment.app.FragmentManager;

import androidx.fragment.app.FragmentTransaction;



import android.annotation.SuppressLint;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.RadioButton;

import android.widget.RadioGroup;



public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    RadioGroup rg;

    RadioButton b1,b2,b3,b4;



    @SuppressLint("MissingInflatedId")

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);



        rg=findViewById(R.id.rg1);

        b1=findViewById(R.id.rb1);

        b2=findViewById(R.id.rb2);

        b3=findViewById(R.id.rb3);

        b4=findViewById(R.id.rb4);



        rg.setOnClickListener(this);

        b1.setOnClickListener(this);

        b2.setOnClickListener(this);

        b3.setOnClickListener(this);

        b4.setOnClickListener(this);

    }



    @Override

    public void onClick(View view) {

           String i= getResources().getResourceEntryName(view.getId());

           switch(i){

               case "rb1":

                   entry_fragment(new BlankFragment1());

                   break;

               case "rb2":

                   entry_fragment(new BlankFragment2());

                   break;

               case "rb3":

                   entry_fragment(new BlankFragment3());

                   break;

               case "rb4":

                   entry_fragment(new BlankFragment4());

                   break;

               default:

                   throw new IllegalStateException("Unexpected value:" +view.getId());

           }

    }



    private void entry_fragment(Fragment f) {

        FragmentManager fm =getSupportFragmentManager();

        FragmentTransaction ft=fm.beginTransaction(); //开始交易

        ft.replace(R.id.fl,f);

        // ft.oddToBackStock(null);

        ft.commit();

    }

}

activity_main_xml
<?xml version="1.0" encoding="utf-8"?>

<androidx.appcompat.widget.LinearLayoutCompat 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"

    android:orientation="vertical"

    tools:context=".MainActivity">



    <FrameLayout

        android:id="@+id/fl"

        android:layout_width="match_parent"

        android:layout_height="0dp"

        android:layout_weight="1"

        android:background="#D3CFCF"></FrameLayout>





    <RadioGroup

            android:id="@+id/rg1"

            android:layout_width="match_parent"

            android:layout_height="50dp"

            android:orientation="horizontal"

            android:layout_marginBottom="5dp"

            >



        <RadioButton

            android:id="@+id/rb1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="首页"

            />



        <RadioButton

            android:id="@+id/rb2"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="发现" />



        <RadioButton

            android:id="@+id/rb3"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="消息" />



        <RadioButton

            android:id="@+id/rb4"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:text="关于" />

    </RadioGroup>


</androidx.appcompat.widget.LinearLayoutCompat>
fragment_blank1
<?xml version="1.0" encoding="utf-8"?>

<FrameLayout 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:background="#ECE8E8"

    tools:context=".BlankFragment1">



    <!-- TODO: Update blank fragment layout -->



    <TextView

        android:id="@+id/textView"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

            android:textSize="50dp"

        android:text="首页" />



</FrameLayout>
fragment_blank2
<?xml version="1.0" encoding="utf-8"?>

<FrameLayout 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:background="#F6F5F5"

    tools:context=".BlankFragment2">



    <!-- TODO: Update blank fragment layout -->



    <TextView

        android:id="@+id/textView2"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:textSize="50dp"

        android:text="发现" />

</FrameLayout>
fragment_blank3
<?xml version="1.0" encoding="utf-8"?>

<FrameLayout 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:background="#F5F8F8"

    tools:context=".BlankFragment3">



    <!-- TODO: Update blank fragment layout -->

    <TextView

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:textSize="50dp"

        android:text="消息" />



</FrameLayout>
fragment_blank4
<?xml version="1.0" encoding="utf-8"?>

<FrameLayout 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:background="#FDFCFD"

    tools:context=".BlankFragment4">



    <!-- TODO: Update blank fragment layout -->

    <TextView

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:textSize="50dp"

        android:text="关于" />



</FrameLayout>

运行结果

            

          

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

零一壹一

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值