使用Fragment:点击不同的按钮,出来不同的Fragment
效果图:
activity的布局文件:
<RelativeLayout 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="440dp"
android:id="@+id/layout"
android:orientation="horizontal"
>
</LinearLayout>
<RadioGroup
android:layout_below="@id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/one"
android:layout_width="0dp"
android:layout_weight="1"
android:button="@null"
android:text="one"
android:gravity="center"
android:textSize="22sp"
android:background="@drawable/back"
android:layout_height="match_parent" />
<RadioButton
android:id="@+id/two"
android:layout_width="0dp"
android:layout_weight="1"
android:button="@null"
android:text="two"
android:gravity="center"
android:textSize="22sp"
android:background="@drawable/back"
android:layout_height="match_parent" />
<RadioButton
android:id="@+id/three"
android:layout_width="0dp"
android:layout_weight="1"
android:button="@null"
android:text="three"
android:gravity="center"
android:background="@drawable/back"
android:textSize="22sp"
android:layout_height="match_parent" />
</LinearLayout>
</RadioGroup>
</RelativeLayout>
activity:
public class MainActivity extends AppCompatActivity {
private LinearLayout layout;
private RadioButton one;
private RadioButton two;
private RadioButton three;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.layout,new MyFragment1());
one.setBackgroundResource(R.drawable.back_check);
transaction.commit();
one.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
one.setBackgroundResource(R.drawable.back_check);
two.setBackgroundResource(R.drawable.back);
three.setBackgroundResource(R.drawable.back);
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.layout,new MyFragment1());
transaction.commit();
}
});
two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
one.setBackgroundResource(R.drawable.back);
two.setBackgroundResource(R.drawable.back_check);
three.setBackgroundResource(R.drawable.back);
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.layout,new MyFragment2());
transaction.commit();
}
});
three.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
one.setBackgroundResource(R.drawable.back);
two.setBackgroundResource(R.drawable.back);
three.setBackgroundResource(R.drawable.back_check);
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.layout,new MyFragment3());
transaction.commit();
}
});
}
private void initView() {
layout = (LinearLayout) findViewById(R.id.layout);
one = (RadioButton) findViewById(R.id.one);
two = (RadioButton) findViewById(R.id.two);
three = (RadioButton) findViewById(R.id.three);
}
}
还有相对应的Fragment所对应的文件
public class MyFragment1 extends Fragment{
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return View.inflate(getActivity(),R.layout.item1,null);
}
}
Fragment:布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="24sp"
android:gravity="center"
android:id="@+id/tv"
android:text="one"
/>
</LinearLayout>