国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android Drawerlayout實現(xiàn)側滑菜單效果

2019-10-22 18:24:59
字體:
來源:轉載
供稿:網(wǎng)友

本文實例為大家分享了Drawerlayout側滑菜單的具體代碼,供大家參考,具體內容如下

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">  <!--側拉菜單-->  <android.support.v4.widget.DrawerLayout    android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/drlayout">    <!--主頁面的布局-->    <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);    //構造數(shù)據(jù)    datas = new ArrayList<String>();    for(int i=0;i<10;i++){      datas.add("菜單"+i);    }    //設置數(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) {        //關閉側滑菜單        drawerLayout.closeDrawer(rel);        //drawerLayout.openDrawer(rel);//打開側滑菜單        //設置參數(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:側滑菜單 寬度的像素值      @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);      }    });  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 武隆县| 商洛市| 新兴县| 涪陵区| 泌阳县| 磴口县| 定结县| 铁岭县| 页游| 宁化县| 牡丹江市| 乡城县| 平谷区| 大余县| 冷水江市| 三都| 大同市| 手游| 北碚区| 桂东县| 昌邑市| 盱眙县| 满洲里市| 贵阳市| 通化市| 大冶市| 昌图县| 郯城县| 安陆市| 澎湖县| 枝江市| 成武县| 罗山县| 留坝县| 乌拉特后旗| 得荣县| 泗阳县| 新源县| 博湖县| 封开县| 阿坝|