Android側滑菜單的實現,android側滑

來源:互聯網
上載者:User

Android側滑菜單的實現,android側滑

一直看別人應用裡面有一個側滑菜單,感覺好酷斃,但是自己也不會寫,那個心情叫做難過了,恨自己不會。我也百度了一段時間,只恨自己的水平不高,看不懂代碼,所以就找了許多的例子,但是還看不懂,終於一次偶然的機會,我找到了,是的,找到了。這個代碼就很簡單了,因為它提供了別人寫的一個類,就是布局類,直接可以在XML中使用,然後就可以直接運行了,至於要加什麼東西,就隨自己的心意了。

好了,不多說了,現在開始講實現的過程,真的很簡單,句相當於提供了一個已經有的空間,你所要做的就是往裡面填代碼,然後就可以運行了。(當時看別人牛逼,是不是他用的也是這個類啊,其實大家也差不多嘛)

給大家看一下:



正如所示,菜單在左邊,向右滑動時,主介面向右移動,菜單慢慢出來,比Android自己提供的那種菜單要炫酷多了。

好了,說了那個類到底是哪個呢?

SlideHolder,它是FrameLayout的子類,也就是說,這個是在XML布局檔案中使用的,如下面的代碼,它是MainActivity的布局代碼:

<span style="font-size:18px;"><com.example.slidemenu_test.SlideHolder xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/holder"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <ScrollView         android:layout_width="200dp"        android:layout_height="fill_parent"        android:background="#000000"        >        <LinearLayout             android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:orientation="vertical"            >            <TextView                 android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:text="菜單1"                android:textColor="#ffffff"                android:textSize="25sp"                />            <TextView                 android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:text="菜單2"                android:textColor="#ffffff"                android:textSize="25sp"                />        </LinearLayout>    </ScrollView>    <LinearLayout         android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:orientation="vertical"        android:background="@drawable/bg"        >        <LinearLayout             android:layout_width="fill_parent"            android:layout_height="50dp"            >            <ImageButton                android:id="@+id/imageButton"                 android:layout_width="50dp"                android:layout_height="50dp"                android:src="@drawable/btn_icon"                android:background="#0000"                />                    </LinearLayout>    </LinearLayout></com.example.slidemenu_test.SlideHolder></span>

接下來我對這個代碼進行說明,在建立一個Android項目的時候,Eclipse會自動產生activity_main.xml代碼,它是MainActivity的布局檔案,我們需要把activity_main.xml裡面的內容換成上面所示,然後就可以直接運行了。在上面的代碼中,首先是ScrollView,也就是菜單,我們往這個布局裡面加入自己想要的內容,我就隨便加了一點,做為測試嘛,接下來是一個布局LinearLayout,這個就是主介面了,也就是不滑動的時候顯示的介面,在這裡面,我加了一個按鈕來控制功能表的彈出,當然也可以不設定,可以直接啟動並執行,但是許多應用都設定了,我也就設定一下,我添加了一個ImageButton,在MainActivity裡面對它進行了設定,下面我們來看MainActivity的代碼(簡單易懂):

package com.example.slidemenu_test;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageButton;public class MainActivity extends Activity {private ImageButton mBtn;private SlideHolder mSlideHolder;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mBtn = (ImageButton) findViewById(R.id.imageButton);mSlideHolder = (SlideHolder) findViewById(R.id.holder);mBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {mSlideHolder.toggle();}});}}
在這個代碼裡,有一個ImageButton對象和一個SlideHolder對象,給ImageButton添加事件,點擊一下就快顯功能表,快顯功能表也比較簡單,就一行代碼mSlideHolder.toggle(); 函數toggle是SlideHolder中的方法大家想瞭解更多的我把源碼下載下來自己多瞭解吧。

好了,至此,一個側滑菜單就出來了(站在別人的肩膀上,我能看的更遠^_^)

源碼:http://download.csdn.net/detail/programchangesworld/8589471點此下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.