布局
<?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:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.bwei.www.monimouth01.MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!--主页面底部布局--> <LinearLayout android:id="@+id/ll_bottom" android:orientation="horizontal" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/txt_concer" android:text="关注" android:textSize="21sp" android:gravity="center" android:layout_weight="1" android:padding="5dp" android:layout_width="0dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/txt_message" android:text="消息" android:textSize="21sp" android:gravity="center" android:layout_weight="1" android:padding="5dp" android:layout_width="0dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/txt_my" android:text="我的" android:textSize="21sp" android:gravity="center" android:layout_weight="1" android:padding="5dp" android:layout_width="0dp" android:layout_height="wrap_content" /> </LinearLayout> <FrameLayout android:layout_width="match_parent" android:id="@+id/fl_content" android:layout_above="@id/ll_bottom" android:layout_height="match_parent" > </FrameLayout> </RelativeLayout> <!--抽屉的背景--> <LinearLayout android:orientation="vertical" android:layout_gravity="start" android:background="#FFFFFF" android:layout_width="320dp" android:layout_height="match_parent"> <ListView android:id="@+id/lv_drawer" android:layout_width="match_parent" android:layout_height="wrap_content"> </ListView> </LinearLayout> </android.support.v4.widget.DrawerLayout>
Activity要写的
import android.support.v4.app.FragmentManager; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private List<String> listLeft; private LeftDrawAdapter adapter; private ConcernFragment cf; private TextView txtConrent; private TextView txtMessage; private TextView txtMy; //关注 private FragmentManager manager; //消息 private MessageFragment msgf; //我的 private MyFragment mf; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找控件 DrawerLayout dl = findViewById(R.id.dl); ListView lvDraewr = findViewById(R.id.lv_drawer); txtConrent = findViewById(R.id.txt_concer); txtMessage = findViewById(R.id.txt_message); txtMy = findViewById(R.id.txt_my); //点击事件 txtConrent.setOnClickListener(this); txtMessage.setOnClickListener(this); txtMy.setOnClickListener(this); //创建一个抽屉里list集合 listLeft = new ArrayList<>(); //抽屉里显示的数据 listLeft.add("所有收件箱"); listLeft.add("所有未读"); listLeft.add("所有红旗"); //抽屉的适配器 adapter = new LeftDrawAdapter(this,listLeft); lvDraewr.setAdapter(adapter); //抽屉下的Fragment的页面 cf = new ConcernFragment(); msgf = new MessageFragment(); mf = new MyFragment(); manager = getSupportFragmentManager(); //当前页面进行设置 manager.beginTransaction(). add(R.id.fl_content,cf) .add(R.id.fl_content,msgf) .add(R.id.fl_content,mf) .hide(msgf) .hide(mf) .commit(); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.txt_concer: manager.beginTransaction() .show(cf) .hide(msgf) .hide(mf) .commit(); break; case R.id.txt_message: manager.beginTransaction() .hide(cf) .show(msgf) .hide(mf) .commit(); break; case R.id.txt_my: manager.beginTransaction() .hide(cf) .hide(msgf) .show(mf) .commit(); break; } } }