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 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;
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;
private RelativeLayout relMenu;
private DrawerLayout drawerLayout;
private RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
imgTitle = (ImageView) findViewById(R.id.img_title);
radioGroup = (RadioGroup) findViewById(R.id.rel_navigate);
//侧滑菜单的视图
relMenu = (RelativeLayout) findViewById(R.id.rel_menu);
relMenu = (RelativeLayout) findViewById(R.id.rel_menu);
imgTitle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//关闭,侧滑菜单
drawerLayout.closeDrawer(relMenu);
}
});
@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());
@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());
addFragment(new MeFragment());
}else if(checkedId==R.id.rb_video){
addFragment(new VideoFragment());
}
addFragment(new VideoFragment());
}
}
});
//默认添加"首页"
addFragment(new IndexFragment());
}
});
//默认添加"首页"
addFragment(new IndexFragment());
}
/**
* 添加fragment到主页面中
* @param fragment
*/
public void addFragment(Fragment fragment){
getSupportFragmentManager().beginTransaction().replace(R.id.main_content,fragment).commit();
* 添加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">
<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">
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">
<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"/>
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>
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"/>
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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是侧边栏"
android:layout_below="@+id/img_title"/>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>