'
接下来就是布局了。activity_main
<?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.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.matianye.projecttwo81.MainActivity"> <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="75dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:orientation="horizontal" tools:layout_editor_absoluteX="8dp" tools:layout_editor_absoluteY="457dp"> <RadioButton android:id="@+id/monight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableTop="@drawable/monight_select" android:layout_weight="1" android:button="@null" android:checked="true" android:gravity="center" android:padding="15dp" android:text="月光恋人" /> <RadioButton android:id="@+id/onsale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableTop="@drawable/onsale_select" android:layout_weight="1" android:button="@null" android:gravity="center" android:padding="15dp" android:text="优惠" /> <RadioButton android:id="@+id/shoppingcar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:gravity="center" android:padding="15dp" android:drawableTop="@drawable/shoppingcar_selcet" android:text="购物车" /> <RadioButton android:id="@+id/myself" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableTop="@drawable/myself_select" android:layout_weight="1" android:button="@null" android:gravity="center" android:padding="15dp" android:text="我的" /> </RadioGroup> <FrameLayout android:id="@+id/fragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/radioGroup"></FrameLayout> </RelativeLayout>
其他的布局内容需要显示的是什么内容,你就写什么内容,这个不再写了。
现在是class类
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private RadioButton r1, r2, r3, r4; private ViewPager mViewPager; private MoNightFragment mMoNightFragment; private OnSaleFragment mOnSaleFragment; private ShoppingCarFragment mShoppingCarFragment; private MySelfFragment mMySelfFragment; private Fragment fragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { r1 = (RadioButton) findViewById(R.id.monight); r2 = (RadioButton) findViewById(R.id.onsale); r3 = (RadioButton) findViewById(R.id.shoppingcar); r4 = (RadioButton) findViewById(R.id.myself); r1.setOnClickListener(this); r2.setOnClickListener(this); r3.setOnClickListener(this); r4.setOnClickListener(this); mMoNightFragment = new MoNightFragment(); mOnSaleFragment = new OnSaleFragment(); mShoppingCarFragment = new ShoppingCarFragment(); mMySelfFragment = new MySelfFragment(); getSupportFragmentManager() .beginTransaction() .replace(R.id.fragment, mMoNightFragment).commit(); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.monight: getSupportFragmentManager() .beginTransaction() .replace(R.id.fragment, mMoNightFragment).commit(); break; case R.id.onsale: getSupportFragmentManager() .beginTransaction() .replace(R.id.fragment, mOnSaleFragment).commit(); break; case R.id.shoppingcar: getSupportFragmentManager() .beginTransaction() .replace(R.id.fragment, mShoppingCarFragment).commit(); break; case R.id.myself: getSupportFragmentManager() .beginTransaction() .replace(R.id.fragment, mMySelfFragment).commit(); break; } } }
其他的fragment也不再写了,获取相对应的fragment布局就可以了。