<?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.daylayout.MainActivity">
<android.support.v4.widget.DrawerLayout
android:id="@+id/laydate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<FrameLayout
android:id="@+id/fram"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<RelativeLayout
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#fff0"
android:id="@+id/rel"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:id="@+id/image"
/>
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
2 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvv"
android:text="我是主页面"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
3.
package com.example.daylayout;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private DrawerLayout layout;
private ListView lv;
private ArrayList<String> strings=new ArrayList<String>();
private RelativeLayout rel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = (DrawerLayout) findViewById(R.id.laydate);
lv = (ListView) findViewById(R.id.lv);
rel = (RelativeLayout) findViewById(R.id.rel);
//构造数据
strings = new ArrayList<>();
for (int i=0;i<10;i++){
strings.add("菜单"+i);
}
//设置适配器
ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,strings);
lv.setAdapter(stringArrayAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//关闭策划
layout.closeDrawer(rel);
//设置参数
ContextFragment contextFragment = new ContextFragment();
Bundle bundle = new Bundle();
bundle.putString("value",strings.get(i));
contextFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.fram,contextFragment).commit();
}
});
layout.setDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
}
@Override
public void onDrawerOpened(View drawerView) {
}
@Override
public void onDrawerClosed(View drawerView) {
}
@Override
public void onDrawerStateChanged(int newState) {
}
});
}
}
4
package com.example.daylayout;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import static android.R.attr.value;
/**
* author:Created by gaoqingqing on 2017/10/13.
*/
public class ContextFragment extends Fragment{
private TextView tvv;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = View.inflate(getActivity(), R.layout.items, null);
tvv = (TextView) v.findViewById(R.id.tvv);
String values = getArguments().getString("value");
tvv.setText(values);
return v;
}
}