android:textSize=“30sp”/>
Test3FragmentActivity
public class Test3FragmentActivity extends AppCompatActivity implements View.OnClickListener {
private FragmentManager manager;
private FragmentTransaction transaction;
@SuppressLint(“CommitTransaction”)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test3_fragment);
initViews();
manager = getFragmentManager();
transaction = manager.beginTransaction();
transaction.add(R.id.content_layout, new ShopRankFragment());
transaction.commit();
}
private void initViews() {
RadioButton rbShopRank = findViewById(R.id.rb_shop_rank);
RadioButton rbShare = findViewById(R.id.rb_share);
RadioButton rbGift = findViewById(R.id.rb_gift);
RadioButton rbOrder = findViewById(R.id.rb_order);
rbShopRank.setOnClickListener(this);
rbShare.setOnClickListener(this);
rbGift.setOnClickListener(this);
rbOrder.setOnClickListener(this);
}
@SuppressLint(“NonConstantResourceId”)
@Override
public void onClick(View v) {
transaction = manager.beginTransaction();
switch (v.getId()) {
case R.id.rb_shop_rank:
transaction.replace(R.id.content_layout,new ShopRankFragment());
break;
case R.id.rb_share:
transaction.replace(R.id.content_layout,new ShareFragment());
break;
case R.id.rb_gift:
transaction.replace(R.id.content_layout,new GiftFragment());
break;
case R.id.rb_order:
transaction.replace(R.id.content_layout,new OrderFragment());
break;
default:
break;
}
transaction.commit();
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout
xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.a
ndroid.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".fragment.Test3FragmentActivity">
<LinearLayout
android:id="@+id/content_layout"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:orientation=“vertical”>
<LinearLayout
android:id="@+id/bottom_layout"
android:orientation=“horizontal”
android:layout_width=“match_parent”
android:layout_height=“120dp”
android:background="#ffffff"
android:layout_alignParentBottom=“true”>
<RadioGroup
android:id="@+id/rg_home"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“horizontal”>
<RadioButton
android:id="@+id/rb_shop_rank"
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:layout_gravity=“center”
android:button="@null"
android:drawableTop="@mipmap/ic_launcher"
android:drawablePadding=“10dp”
android:gravity=“center”
android:text="@string/home_shop"
android:textColor="#B3B3B3"
android:textSize=“15sp” />
<RadioButton
android:id="@+id/rb_share"
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:layout_gravity=“center”
android:button="@null"
android:drawableTop="@mipmap/ic_launcher"
android:d