Android抽屉页面效果

DrawerLayout抽屉布局,如今主流App是愈来愈多使用DrawerLayout,由于这样出来的效果是比较炫酷的吧!其实抽屉界面很简单,没有网上说的那么复杂,今天我就给你们介绍一种比较简单的抽屉布局,DrawerLayout,android


效果图:app



1:主布局文件(分为2块,第一块是主页面内容,第二块是抽屉页面内容,这是固定的,必须是这样的格式)ide

<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能够添加一个布局文件spa


       
<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 >

xml

</LinearLayout >utf-8

2:Fragment布局文件get

<?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="按钮四"/>


it

</LinearLayout >io

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); //从左打开

            }
        });
    }

}

相关文章
相关标签/搜索