教你用DrawLayout 實現Android 側滑菜單

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   color   sp   strong   

現在的APP越來越注重使用者體驗,百度視頻用戶端有一個特效還是挺迷人的,在主介面手指向右滑動,就可以將菜單展示出來,而主介面會被隱藏大部分,但是仍有左側的一小部分同菜單一起展示。類似的還有天天動聽,人人的用戶端。

這個側滑菜單的實現網上也有很多方法,比如最常用的是開源的 SlidingMenu . 還有一種實現方式是在一個Activity的布局中分兩部分,一個是菜單(menu)的布局,一個是內容(content)的布局。兩個布局橫向排列,菜單布局在左,內容布局在右。初始化的時候將菜單布局向左位移,以至於能夠完全隱藏,這樣內容布局就會完全顯示在Activity中。然後通過監聽手指滑動事件,來改變菜單布局的左位移距離,從而控制功能表布局的顯示和隱藏。


但是這兩種方法都相對比較繁瑣,今天給大家介紹一種更為簡單的方法。就是直接採用DrawLayout。有關DrawLayout更詳細的介紹可以參考API文檔:http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html  。

首先New一個Android工程,依次實現幾個布局。先來看兩個子布局。


    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

  
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="#00000000"
        android:divider="@android:color/transparent"/>



    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

        android:id="@+id/scrollView1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" >

    
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="每日一文"
        android:textAppearance="?android:attr/textAppearanceLarge" >
  

主介面布局,注釋掉的是右邊的側滑,現在實現的是左邊的側滑。

       xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

            android:id="@+id/fragment_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
   

            android:id="@+id/menu_layout_left"
        android:layout_width="150dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="#FFFFFF" >

                    android:id="@+id/menu_listView_l"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
       
   

  



然後建立兩個類繼承Fragment,把兩個子布局塞進去。
public class FirstFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.first, null);
    }
}

再來看看主類:
public class MainActivity extends FragmentActivity {

 
    public static final String[] TITLES = {"first", "second"};
    private DrawerLayout mDrawerLayout;
    private RelativeLayout mLeftLayout;
    private ListView mLeftListView;
   

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

        findViewById();
        mLeftListView.setAdapter(new ArrayAdapter(this,
                android.R.layout.simple_expandable_list_item_1, TITLES));


        // 監聽菜單
        mLeftListView.setOnItemClickListener(new DrawerItemClickListenerLeft());
    }

    private void findViewById() {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mLeftLayout = (RelativeLayout) findViewById(R.id.menu_layout_left);
        mLeftListView = (ListView) findViewById(R.id.menu_listView_l);
    }

   
   public class DrawerItemClickListenerLeft implements OnItemClickListener {

        @Override
        public void onItemClick(AdapterView

 

教你用DrawLayout 實現Android 側滑菜單

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.