Android中如何使用DrawerLayout側(cè)滑控件-創(chuàng)新互聯(lián)

Android中如何使用DrawerLayout側(cè)滑控件,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)公司從2013年成立,先為宜君等服務(wù)建站,宜君等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢(xún)服務(wù)。為宜君企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。

activity_sliding.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/main_drawer_layout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@android:color/transparent">

 <!-- 下面顯示的主要是主界面內(nèi)容 -->
 <RelativeLayout
  android:id="@+id/main_content_frame_parent"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@android:color/transparent"
  android:gravity="center">

  <Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:onClick="openLeftLayout"
   android:text="左邊" />

  <Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:layout_marginLeft="100dp"
   android:onClick="openRightLayout"
   android:text="右邊" />

 </RelativeLayout>

 <!-- 左側(cè)滑動(dòng)欄 -->
 <RelativeLayout
  android:id="@+id/main_left_drawer_layout"
  android:layout_width="240dp"
  android:layout_height="match_parent"
  android:layout_gravity="start"
  android:background="@color/colorPrimary"
  android:paddingTop="50dp">

  <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerInParent="true"
   android:text="左邊菜單測(cè)試" />

 </RelativeLayout>

 <!-- 右側(cè)滑動(dòng)欄 -->
 <RelativeLayout
  android:id="@+id/main_right_drawer_layout"
  android:layout_width="240dp"
  android:layout_height="match_parent"
  android:layout_gravity="end"
  android:background="@color/colorPrimary"
  android:paddingTop="50dp">

  <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerInParent="true"
   android:text="右邊菜單測(cè)試" />
 </RelativeLayout>


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

通過(guò)上面的布局文件我們發(fā)現(xiàn) drawerlayout中的子布局分為content、left、right三部分,其中l(wèi)eft和right的布局需要在layout中聲明android:layout_gravity屬性,值分別是start和end。很顯然,drawerlayout布局類(lèi)似一個(gè)大容器,超屏布局,將left的布局放在了控件的開(kāi)始地方,right的布局放在了控件結(jié)尾的地方。

DrawerSlidingActivity.java:

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RelativeLayout;

public class DrawwerSlidingActivity extends AppCompatActivity {

 // 抽屜菜單對(duì)象
 private ActionBarDrawerToggle drawerbar;
 public DrawerLayout drawerLayout;
 private RelativeLayout main_left_drawer_layout, main_right_drawer_layout;

 @Override
 protected void onCreate(Bundle arg0) {
  super.onCreate(arg0);
  setContentView(R.layout.activity_slidingmenu);

  initLayout();
  initEvent();
 }

 public void initLayout() {
  drawerLayout = (DrawerLayout) findViewById(R.id.main_drawer_layout);

  //設(shè)置菜單內(nèi)容之外其他區(qū)域的背景色
  drawerLayout.setScrimColor(Color.TRANSPARENT);

  //左邊菜單
  main_left_drawer_layout = (RelativeLayout) findViewById(R.id.main_left_drawer_layout);
  //右邊菜單
  main_right_drawer_layout = (RelativeLayout) findViewById(R.id.main_right_drawer_layout);

 }

 //設(shè)置開(kāi)關(guān)監(jiān)聽(tīng)
 private void initEvent() {
  drawerbar = new ActionBarDrawerToggle(this, drawerLayout, R.mipmap.ic_launcher, R.string.open, R.string.close) {
   //菜單打開(kāi)
   @Override
   public void onDrawerOpened(View drawerView) {
    super.onDrawerOpened(drawerView);
   }

   // 菜單關(guān)閉
   @Override
   public void onDrawerClosed(View drawerView) {
    super.onDrawerClosed(drawerView);
   }
  };

  drawerLayout.setDrawerListener(drawerbar);
 }

 //左邊菜單開(kāi)關(guān)事件
 public void openLeftLayout(View view) {
  if (drawerLayout.isDrawerOpen(main_left_drawer_layout)) {
   drawerLayout.closeDrawer(main_left_drawer_layout);
  } else {
   drawerLayout.openDrawer(main_left_drawer_layout);
  }
 }

 // 右邊菜單開(kāi)關(guān)事件
 public void openRightLayout(View view) {
  if (drawerLayout.isDrawerOpen(main_right_drawer_layout)) {
   drawerLayout.closeDrawer(main_right_drawer_layout);
  } else {
   drawerLayout.openDrawer(main_right_drawer_layout);
  }
 }
}

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。

當(dāng)前標(biāo)題:Android中如何使用DrawerLayout側(cè)滑控件-創(chuàng)新互聯(lián)
網(wǎng)址分享:http://bm7419.com/article4/cdicoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷(xiāo)型網(wǎng)站建設(shè)關(guān)鍵詞優(yōu)化、靜態(tài)網(wǎng)站微信小程序、外貿(mào)建站、ChatGPT

廣告

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

h5響應(yīng)式網(wǎng)站建設(shè)