您好,登錄后才能下訂單哦!
Android中如何使用DrawerLayout側(cè)滑控件,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
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è)滑動欄 --> <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> <!-- 右側(cè)滑動欄 --> <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的布局放在了控件結(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 { // 抽屜菜單對象 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è)置開關(guān)監(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); } // 菜單關(guān)閉 @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); } }; drawerLayout.setDrawerListener(drawerbar); } //左邊菜單開關(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); } } // 右邊菜單開關(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)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。