Radiogroup+DrawerLayout切换效果

布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/dl"
    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.a.moni.MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/fl"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/rg"
        ></FrameLayout>

    <RadioGroup
        android:id="@+id/rg"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="60dp">
        <RadioButton
            android:id="@+id/rb1"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:textSize="16sp"
            android:layout_weight="1"
            android:background="@drawable/b1"
            android:button="@null"
            android:checked="true"
            android:gravity="center"
            android:padding="10dp"
            android:text="首頁" />

        <RadioButton
            android:id="@+id/rb2"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:textSize="16sp"
            android:layout_weight="1"
            android:background="@drawable/b1"
            android:button="@null"
            android:gravity="center"
            android:padding="10dp"
            android:text="发现" />

        <RadioButton
            android:id="@+id/rb3"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:textSize="16sp"
            android:layout_weight="1"
            android:background="@drawable/b1"
            android:button="@null"
            android:gravity="center"
            android:padding="10dp"
            android:text="下载" />

        <RadioButton
            android:id="@+id/rb4"
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:textSize="16sp"
            android:layout_weight="1"
            android:background="@drawable/b1"
            android:button="@null"
            android:padding="10dp"
            android:text="我的" />

    </RadioGroup>
</RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="#ffffff">

        <ImageView
            android:id="@+id/image_touxiang"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_centerHorizontal="true"
            android:layout_margin="10dp"
            android:src="@mipmap/ic_launcher" />

        <ListView
            android:id="@+id/list_view_drawer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/image_touxiang">

        </ListView>
    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>


activity

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.example.a.moni.fragment.Faxian;
import com.example.a.moni.fragment.Shouye;
import com.example.a.moni.fragment.Wode;
import com.example.a.moni.fragment.Xiazai;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {

    private FrameLayout fl;
    private  RadioGroup rg;
    private DrawerLayout dl;
    private RelativeLayout rl;
    private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fl = (FrameLayout) findViewById(R.id.fl);
        dl = (DrawerLayout) findViewById(R.id.dl);        
        rl = (RelativeLayout) findViewById(R.id.rl);
        lv = (ListView) findViewById(R.id.list_view_drawer);
        List<String> list=new ArrayList<>();
        list .add("钱包");
        list .add("行程记录");
        list .add("好友邀请");
        list .add("设置");
        list .add("购买特权卡");

        ArrayAdapter<String> adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,android.R.id.text1,list);
        lv.setAdapter(adapter);
        rg=(RadioGroup)findViewById(R.id.rg);
        
        getSupportFragmentManager().beginTransaction().replace(R.id.fl, new Shouye()).commit();
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i) {
                    case R.id.rb1:
                        getSupportFragmentManager().beginTransaction().replace(R.id.fl, new Shouye()).commit();
                        break;
                    case R.id.rb2:
                        getSupportFragmentManager().beginTransaction().replace(R.id.fl, new Faxian()).commit();
                        break;
                    case R.id.rb3:
                        getSupportFragmentManager().beginTransaction().replace(R.id.fl, new Xiazai()).commit();
                        break;
                    case R.id.rb4:
                        getSupportFragmentManager().beginTransaction().replace(R.id.fl, new Wode()).commit();
                        break;

                    default:
                        break;
                }
            }
        });

    }
}

发布了8 篇原创文章 · 获赞 0 · 访问量 2347
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览