1. 程式人生 > >DrawerLayout實現簡單的側滑功能

DrawerLayout實現簡單的側滑功能

專案要實現類似於網易新聞客戶端的側滑拉出選單的功能,搜了好些資料,有下面的三種方法:

1)自定義viewgroup

2)匯入開源專案slidingmenu_library

3)採用V4包的元件DrawerLayout

第三種方法是最方便快捷的了,雖然第三種方法不能很好地支援低版本安卓手機,但是因為我們也沒有這種需求,所以最後我還是決定採用DrawerLayout。不過第一種方法是最好的(雖然從開發效率上來說不是),有空自己還得要學一下自己造輪子。

DrawerLayout的xml:

<RelativeLayout 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=".MainActivity" >

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <Button
                android:id="@+id/btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="open" />
        </FrameLayout>

        <LinearLayout
            android:id="@+id/left_layout"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:background="#FFB5C5"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1"
                android:textSize="20sp" >
            </TextView>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2"
                android:textSize="20sp" >
            </TextView>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="3"
                android:textSize="20sp" >
            </TextView>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="4"
                android:textSize="20sp" >
            </TextView>
        </LinearLayout>
    </android.support.v4.widget.DrawerLayout>

</RelativeLayout>

其中包裹了2個或者以上的view,需要注意的是第一個view是主頁面的view,後面的才是側滑出頁面的view。主頁面view的寬高要寫成
android:layout_width="match_parent"
android:layout_height="match_parent"

因為在沒有側滑的時候是要包裹父view窗體的。

而在側滑出的頁面的view要設定layout_gravity的值指明是向左滑出還是向右滑出。

下面的Activity,比較簡單:

package com.example.drawerlayout;

import android.os.Bundle;
import android.app.Activity;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
	private DrawerLayout mDrawerLayout = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

		Button button = (Button) findViewById(R.id.btn);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 按鈕按下,將抽屜開啟
				mDrawerLayout.openDrawer(Gravity.LEFT);
			}
		});
	}

}

直接呼叫openDrawer就可以拉開側滑頁面了。需要注意的是後面也要寫明側滑方向(LEFT),並且與前面xml標明的方向要一致,不然可能會出錯。

DrawerLayout還有很多很好用的API,我還在研究中所以就不貼出來了。

相關推薦

DrawerLayout實現簡單功能

專案要實現類似於網易新聞客戶端的側滑拉出選單的功能,搜了好些資料,有下面的三種方法: 1)自定義viewgroup 2)匯入開源專案slidingmenu_library 3)採用V4包的元件DrawerLayout 第三種方法是最方便快捷的了,雖然第三種方法不能很好地支援

使用DrawerLayout實現簡單效果

簡介 DrawerLayout是官方的一個實現側滑選單的控制元件,可以使用它實現大部分的側滑效果。 基本步驟 首先在佈局中使用DrawerLayout,它繼承自VIewGroup,第一個childView就是我們的內容佈局,第二個就是我們的選單,當然也

iOS,(最簡單效果卻很好的功能實現

很多時候側滑功能都會被用到。下面來介紹一下,很簡單,效果卻很好。 1.先按照圖片新建LeftViewController,MainViewController(當然請先下載ReSideMenu類:http://download.csdn.net/detail/qq_2732

簡單的方法實現功能

首先新建一個類 package com.example.util; import android.content.Context; import android.util.AttributeSet; import android.util.TypedValue; imp

微信小程式使用swiper實現功能

在很多的框架中,都有一個側滑的元件,命名基本都是叫toolbar之類的,而且他的功能也是基本類似, 使用swiper的話,可以實現它的側滑功能,具體實現如下: <view>點選下方

使用DrawerLayout和Toolbar實現選單修改預設圖示

兩步: 1,設定ActionBarDrawerToggle toggle.setDrawerIndicatorEnabled(false); 2,設定Toolbar toolbar.s

如何用CSS和jQuery實現一個導航菜單

his 事件監聽 也有 font ajax size add tar 出現 為了建立導航菜單,讓我們先看看html結構:<!DOCTYPE html><html lang="en"><head> <meta charset="U

實現簡單圖片下載功能及防盜鏈

nco eth coder lose ring || pro rect 下載 1.防盜鏈的基本原理就是就是一句話:通過判斷request請求頭的refer是否來源於本站。 2.HTTP Referer是header的一部分,當瀏覽器向web服務器發送請求的時候,一般會帶上R

Java中使用UDP實現簡單的聊天功能

udp http @override 簡單的 ima string [] 發送消息 sock 通過DatagramSocket類來實現。此類表示用來發送和接收數據報包的套接字。 發送端代碼如下: 2 3 import java.io.IOException; 4

使用socket.io實現簡單的聊天功能

ins sock UNC say mes 輪詢 var document style Socket.io實際上是WebSocket的父集,Socket.io封裝了WebSocket和輪詢等方法 首先得在你的項目中安裝socket.io $ npm install sock

ORACLE+Myeclipse+jsp實現簡單登錄功能

public man drivers asus style 編寫 ole 建立 rom Oracle數據庫表的建立: create table Log(    un char(20), pd char(25) ) ta

python學習之路 實現簡單的計算機功能

內部 表達式 remove 符號 加減 per multipl 計算機 print 計算器的主要思維是: 1、對輸入的表達式去除其空格 2、判斷用戶輸入的表達式中的括號是否合法 3、每次找到這個表達式只有一對括號的表達式(就是這找到的表達式內部沒有括號) 4、對找到的只有一

vue實現簡單的購物車功能

src put turn tps html ted lis app content <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" />

安卓常用控制元件RecyclerView+HorizontalScrollView實現item效果 安卓常用控制元件RecyclerView+HorizontalScrollView實現item效果

原 安卓常用控制元件RecyclerView+HorizontalScrollView實現item側滑效果 2017年10月28日 12:23:14 低-調

html實現簡單的計算功能與實時更新時間

簡單的計算器 實時更新時間 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>線上計算器</title> <s

ThinkPHP5實現簡單的登入功能

public function verification() { // 判斷是否是POST登入,如果不是,直接殺掉 if(!isset($_POST['submit'])){ $this->error("非法訪問!!!"); } $username = $_PO

scrollTop,offsetTop,scrollIntoView配合實現簡單的定位功能

js的各種offsetXXX和scrollXXX的介紹非常多,其使用也非常廣泛,特別是在需要對頁面定位的時候,最近實現一個簡單的目錄點選定位內容及滾動螢幕定位目錄標題的小功能,完全是靠這兩個屬性實現,當然事件監聽啥的,這肯定是必要的。 直接上程式碼: 一、頁面滾動,自動定

實現Activity返回效果

先看一張效果圖 通過ViewDragHelper來檢測到螢幕側滑,然後通過內建介面傳遞給Acitivity觸發了側滑事件,通知其關閉。 1、實現側滑刪除,這裡的方法是先要建立一個監聽側滑的自定義佈局. public class SwipeBackLayout exten

HTML-css-JavaScript實現簡單登入註冊功能,並讓登入和註冊在同一頁內切換。

(水平有限,如果發現有什麼問題,歡迎指出來) HTML+css先做出來一個登入頁,包括基本的輸入框和button。再做一個註冊頁。程式碼如下: 隨後解釋程式碼 <form id="register" name="myForm" class="register"

MFC實現簡單的聊天功能

設計目標: 2.設計二  簡單的即時通訊軟體(6學時實驗) 目的與要求: 利用WinSock進行點對點通訊,工作機制模仿即時通訊軟體的基本功能,登陸,上線,傳遞資訊等等。分為客戶部分和伺服器部分兩塊,客戶部分類似一般通訊軟體例如QQ,伺服器部分主要提供客戶端使用者基本資料