工具:Android Studio
目录
问题描述
底部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>
运行结果