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); //從左打開

            }
        });
    }

}

相關文章
相關標籤/搜索