Fragment

package animtest.com.example.e531.yuekao_test_demo;

import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import animtest.com.example.e531.yuekao_test_demo.fragments.IndexFragment;
import animtest.com.example.e531.yuekao_test_demo.fragments.MeFragment;
import animtest.com.example.e531.yuekao_test_demo.fragments.TopFragment;
import animtest.com.example.e531.yuekao_test_demo.fragments.VideoFragment;

public class MainActivity extends AppCompatActivity {

    private ImageView imgTitle;
    private RelativeLayout relMenu;
    private DrawerLayout drawerLayout;
    private RadioGroup radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        drawerLayout = (DrawerLayout) findViewById(R.id.mydrawer);
        imgTitle = (ImageView) findViewById(R.id.img_title);
        radioGroup = (RadioGroup) findViewById(R.id.rel_navigate);

        //侧滑菜单的视图
        relMenu = (RelativeLayout) findViewById(R.id.rel_menu);

        imgTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //关闭,侧滑菜单
                drawerLayout.closeDrawer(relMenu);
            }
        });

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId==R.id.rb_index){
                    Log.d("zzz","add index fragment ***********");
                    addFragment(new IndexFragment());
                }else if(checkedId==R.id.rb_top){
                    addFragment(new TopFragment());

                }else if(checkedId==R.id.rb_me){
                    addFragment(new MeFragment());

                }else if(checkedId==R.id.rb_video){
                    addFragment(new VideoFragment());
                }

            }
        });
        //默认添加"首页"
        addFragment(new IndexFragment());
    }

    /**
     * 添加fragment到主页面中
     * @param fragment
     */
    public  void addFragment(Fragment fragment){
        getSupportFragmentManager().beginTransaction().replace(R.id.main_content,fragment).commit();

    }
}


布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="animtest.com.example.e531.yuekao_test_demo.MainActivity">

    <android.support.v4.widget.DrawerLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/mydrawer">

        <!--主内容区域-->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <RadioGroup
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:id="@+id/rel_navigate"
                android:layout_alignParentBottom="true">
                <RadioButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="首页"
                    android:button="@null"
                    android:gravity="center"
                    android:id="@+id/rb_index"
                    android:padding="3dp"
                    android:background="@drawable/rb_selector"
                    android:checked="true"/>
                <RadioButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="视频"
                    android:button="@null"
                    android:padding="3dp"
                    android:gravity="center"
                    android:id="@+id/rb_video"
                    android:background="@drawable/rb_selector"/>
                <RadioButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="微头条"
                    android:button="@null"
                    android:gravity="center"
                    android:padding="3dp"
                    android:id="@+id/rb_top"
                    android:background="@drawable/rb_selector"/>
                <RadioButton
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="我的"
                    android:padding="3dp"
                    android:button="@null"
                    android:gravity="center"
                    android:id="@+id/rb_me"
                    android:background="@drawable/rb_selector"/>

            </RadioGroup>

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_above="@id/rel_navigate"
                android:id="@+id/main_content"></FrameLayout>


        </RelativeLayout>

        <RelativeLayout
            android:layout_width="260dp"
            android:layout_height="match_parent"
            android:id="@+id/rel_menu"
            android:layout_gravity="start"
            android:background="#550000ff">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher"
                android:id="@+id/img_title"
                android:layout_marginBottom="50dp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="这是侧边栏"
                android:layout_below="@+id/img_title"/>
        </RelativeLayout>


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


</RelativeLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值