AndroidDrawerlayout實(shí)現(xiàn)側(cè)滑菜單效果

本文實(shí)例為大家分享了Drawerlayout側(cè)滑菜單的具體代碼,供大家參考,具體內(nèi)容如下

創(chuàng)新互聯(lián)專注于潞城網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供潞城營(yíng)銷型網(wǎng)站建設(shè),潞城網(wǎng)站制作、潞城網(wǎng)頁(yè)設(shè)計(jì)、潞城網(wǎng)站官網(wǎng)定制、微信小程序定制開(kāi)發(fā)服務(wù),打造潞城網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供潞城網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。

1、Drawerlayout的xml布局

<?xml version="1.0" encoding="utf-8"?>
<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="animtest.com.example.e531.drawerlayout_demo.MainActivity">
  <!--側(cè)拉菜單-->
  <android.support.v4.widget.DrawerLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drlayout">

    <!--主頁(yè)面的布局-->

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

    </FrameLayout>
    <!--拉菜單的布局  android:layout_gravity="start" 菜單在左邊 end 菜單在右邊 -->
    <RelativeLayout
      android:layout_width="240dp"
      android:layout_height="match_parent"
      android:layout_gravity="end"
      android:background="#ffff00"
      android:id="@+id/rel">

      <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:id="@+id/img"/>

      <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv"
        android:layout_below="@id/img"></ListView>

    </RelativeLayout>

  </android.support.v4.widget.DrawerLayout>

</RelativeLayout>

2、在Activity中的具體使用方法

package animtest.com.example.e531.drawerlayout_demo;

import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

  private ListView listView;
  private DrawerLayout drawerLayout;
  private RelativeLayout rel;
  private List<String> datas;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //查找組件
    drawerLayout = (DrawerLayout) findViewById(R.id.drlayout);
    listView = (ListView) findViewById(R.id.lv);
    rel = (RelativeLayout) findViewById(R.id.rel);

    //構(gòu)造數(shù)據(jù)
    datas = new ArrayList<String>();
    for(int i=0;i<10;i++){
      datas.add("菜單"+i);
    }

    //設(shè)置數(shù)組適配器
    ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, datas);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //關(guān)閉側(cè)滑菜單
        drawerLayout.closeDrawer(rel);
        //drawerLayout.openDrawer(rel);//打開(kāi)側(cè)滑菜單

        //設(shè)置參數(shù)
        ContentFragment contentFragment=new ContentFragment();
        Bundle bundle=new Bundle();
        bundle.putString("value",datas.get(position));
        contentFragment.setArguments(bundle);
        //切換fragment
        getSupportFragmentManager().beginTransaction().replace(R.id.fcontent,contentFragment).commit();




      }
    });

    drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {

      //slideOffset:側(cè)滑菜單 寬度的像素值
      @Override
      public void onDrawerSlide(View drawerView, float slideOffset) {

      }

      @Override
      public void onDrawerOpened(View drawerView) {
        Log.d("zzz","open");

      }

      @Override
      public void onDrawerClosed(View drawerView) {
        Log.d("zzz","close");
      }

      @Override
      public void onDrawerStateChanged(int newState) {
        Log.d("zzz","狀態(tài)改變:"+newState);

      }
    });
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)站欄目:AndroidDrawerlayout實(shí)現(xiàn)側(cè)滑菜單效果
當(dāng)前路徑:http://muchs.cn/article14/gesode.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、服務(wù)器托管、響應(yīng)式網(wǎng)站軟件開(kāi)發(fā)、外貿(mào)網(wǎng)站建設(shè)、域名注冊(cè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)