DrawerLayout抽屉布局,现在主流App是越来越多使用DrawerLayout,因为这样出来的效果是比较炫酷的吧!其实抽屉界面很简单,没有网上说的那么复杂,今天我就给大家介绍一种比较简单的抽屉布局,DrawerLayout,
效果图:
1:主布局文件(分为2块,第一块是主页面内容,第二块是抽屉页面内容,这是固定的,必须是这样的格式)
<LinearLayout 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"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<android.support.v4.widget.DrawerLayout
android :id="@+id/drawer_layout"
android :layout_width="match_parent"
android :layout_height="match_parent">
<!-- 主界面-->
<LinearLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000">
<Button
android:id= "@+id/btn"
android:layout_width= "match_parent"
android:layout_height= "wrap_content"
android:text= "open" />
</LinearLayout >
<!-- 抽屉界面 --> //抽屉页面可以是一个Fragment,或者是ListView,什么都可以,这里以Fragment为列子,因为Fragment可以添加一个布局文件
<FrameLayout
android:id="@+id/fragment_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"//从左边滑动进来,右边是end,一定要写,不然没有效果
android:background="#9053ff59"></ FrameLayout>
</android.support.v4.widget.DrawerLayout >
</LinearLayout >
2:Fragment布局文件
<?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"
android:orientation="vertical" >
<Button
android :layout_width="match_parent"
android :layout_height="wrap_content"
android :text="按钮一"/>
<Button
android :layout_width="match_parent"
android :layout_height="wrap_content"
android :text="按钮二"/>
<Button
android :id="@+id/item_frgment_bt"
android :layout_width="match_parent"
android :layout_height="wrap_content"
android :text="按钮三"/>
<Button
android :layout_width="match_parent"
android :layout_height="wrap_content"
android :text="按钮四"/>
</LinearLayout >
4:Fragment类
package com.example.yangjie.drawerlayout;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
/**
* Created by Administrator on 2015/10/17.
*/
public class DrawerFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.item_frgement,container,false);//将布局文件绑定到Fragment中
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super .onViewCreated(view, savedInstanceState);
Button button=(Button)view.findViewById(R.id.item_frgment_bt);
button.setOnClickListener(new View.OnClickListener() { //给抽屉界面的按钮散设置了一个箭头
@Override
public void onClick(View v) {
Toast.makeText (getActivity(),"点击了按钮3" ,Toast.LENGTH_SHORT).show();
}
});
}
}
5:Activity
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout = null ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction ft=fragmentManager.beginTransaction();
DrawerFragment drawerFragment=new DrawerFragment();
ft.add(R.id.fragment_layout,drawerFragment,"标签1");//将fragment绑定到已经占有位置的FrameLayout中
ft.commit();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 按钮按下,将抽屉打开
mDrawerLayout.openDrawer(Gravity. LEFT); //从左打开
}
});
}
}
来源:oschina
链接:https://my.oschina.net/u/2502508/blog/537409