Android中怎么使用DrawerLayout側滑控件-創(chuàng)新互聯(lián)

這篇文章主要講解了“Android中怎么使用DrawerLayout側滑控件”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Android中怎么使用DrawerLayout側滑控件”吧!

十載的南宮網(wǎng)站建設經(jīng)驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。成都全網(wǎng)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設備顯示端的尺寸不同,自動調整南宮建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“南宮網(wǎng)站設計”,“南宮網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。

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">

 <!-- 下面顯示的主要是主界面內容 -->
 <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>

 <!-- 左側滑動欄 -->
 <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="左邊菜單測試" />

 </RelativeLayout>

 <!-- 右側滑動欄 -->
 <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="右邊菜單測試" />
 </RelativeLayout>


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

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


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 {

 // 抽屜菜單對象
 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);

  //設置菜單內容之外其他區(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);

 }

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

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

  drawerLayout.setDrawerListener(drawerbar);
 }

 //左邊菜單開關事件
 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);
  }
 }

 // 右邊菜單開關事件
 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);
  }
 }
}

感謝各位的閱讀,以上就是“Android中怎么使用DrawerLayout側滑控件”的內容了,經(jīng)過本文的學習后,相信大家對Android中怎么使用DrawerLayout側滑控件這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián)網(wǎng)站建設公司,,小編將為大家推送更多相關知識點的文章,歡迎關注!

網(wǎng)站標題:Android中怎么使用DrawerLayout側滑控件-創(chuàng)新互聯(lián)
本文來源:http://muchs.cn/article18/dspdgp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護、營銷型網(wǎng)站建設、網(wǎng)站制作小程序開發(fā)、面包屑導航、網(wǎng)站導航

廣告

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

外貿(mào)網(wǎng)站建設