如何在Android中使用DrawerLayout側(cè)滑控件?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站制作、成都網(wǎng)站建設(shè)與策劃設(shè)計(jì),慶元網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:慶元等地區(qū)。慶元做網(wǎng)站價(jià)格咨詢:18980820575DrawerLayout 類的結(jié)構(gòu)圖如下:
官方中文簡介大概如下:
DrawerLayout作為窗口內(nèi)容的頂層容器,允許從窗口的一個或兩個垂直邊緣拉出交互式“抽屜”視圖。
抽屜定位和布局使用android:layout_gravity 子視圖對應(yīng)的屬性進(jìn)行控制,對應(yīng)于您希望抽屜從哪個側(cè)面出現(xiàn):左側(cè)或右側(cè)(或支持布局方向的平臺版本上的開始/結(jié)束)。請注意,您只能窗口的每個垂直邊緣的一個抽屜視圖。如果您的布局在窗口的每個垂直邊緣配置多個抽屜視圖,則會在運(yùn)行時(shí)拋出異常。
3、DrawerLayout 的使用
一般使用 DrawerLayout 側(cè)滑菜單,可以通過 Toolbar + DrawerLayout來實(shí)現(xiàn)。
在res/layout 中,新建一個toolbar.xml文件
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/tl_custom" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:minHeight="?attr/actionBarSize" android:popupTheme="@style/ThemeOverlay.AppCompat.Dark" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> </android.support.v7.widget.Toolbar>
在新建一個drawerlayout.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/dl_left" android:layout_width="match_parent" android:layout_height="match_parent"> <!--主布局--> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> </LinearLayout> <!--側(cè)滑菜單--> <RelativeLayout android:layout_width="180dp" android:clickable="true" android:layout_height="match_parent" android:layout_gravity="left" android:background="#fff" > <LinearLayout android:id="@+id/ll_home_navigation_top" android:layout_width="match_parent" android:layout_height="120dp" android:layout_alignParentTop="true" android:background="@color/colorPrimary" android:gravity="center" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@mipmap/ic_launcher" /> </LinearLayout> <ListView android:id="@+id/lv_left_menu" android:layout_below="@id/ll_home_navigation_top" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@null" android:text="DrawerLayout" /> </RelativeLayout> </android.support.v4.widget.DrawerLayout>
主布局activity_main.xml:
<LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity"> <!--Toolbar--> <include layout="@layout/custom_toolbar" /> <!--DrawerLayout--> <include layout="@layout/custom_drawerlayout" /> </LinearLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity { protected boolean statusBarCompat = true; private static long DOUBLE_CLICK_TIME = 0L; //聲明相關(guān)變量 private Toolbar toolbar; private DrawerLayout mDrawerLayout; private ActionBarDrawerToggle mDrawerToggle; private ListView lvLeftMenu; private String[] lvs = {"Android", "iOS", "Python", "Html5", "Java"}; private ArrayAdapter arrayAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (statusBarCompat) { StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.colorPrimary)); transparent19and20(); } findViews(); //獲取控件 toolbar.setTitle("Drawerlayout");//設(shè)置Toolbar標(biāo)題 toolbar.setTitleTextColor(Color.parseColor("#ffffff")); //設(shè)置標(biāo)題顏色 setSupportActionBar(toolbar); getSupportActionBar().setHomeButtonEnabled(true); //設(shè)置返回鍵可用 getSupportActionBar().setDisplayHomeAsUpEnabled(true); //創(chuàng)建返回鍵,并實(shí)現(xiàn)打開關(guān)/閉監(jiān)聽 mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.open, R.string.close) { @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); } @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); } }; mDrawerToggle.syncState(); mDrawerLayout.addDrawerListener(mDrawerToggle); //設(shè)置菜單列表 arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, lvs); lvLeftMenu.setAdapter(arrayAdapter); lvLeftMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(MainActivity.this, "點(diǎn)擊"+position , Toast.LENGTH_SHORT).show(); } }); } //返回鍵監(jiān)聽 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_MENU && mDrawerLayout != null) { if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) { mDrawerLayout.closeDrawer(Gravity.LEFT); } else { mDrawerLayout.openDrawer(Gravity.LEFT); } return true; } else if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) { mDrawerLayout.closeDrawer(Gravity.LEFT); } else { if ((System.currentTimeMillis() - DOUBLE_CLICK_TIME) > 2000) { Toast.makeText(MainActivity.this, "再按一次退出", Toast.LENGTH_SHORT).show(); DOUBLE_CLICK_TIME = System.currentTimeMillis(); } else { finish(); } } return true; } return super.onKeyDown(keyCode, event); } protected void transparent19and20() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } } private void findViews() { toolbar = (Toolbar) findViewById(R.id.tl_custom); mDrawerLayout = (DrawerLayout) findViewById(R.id.dl_left); lvLeftMenu = (ListView) findViewById(R.id.lv_left_menu); } }
關(guān)于如何在Android中使用DrawerLayout側(cè)滑控件問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
網(wǎng)頁名稱:如何在Android中使用DrawerLayout側(cè)滑控件-創(chuàng)新互聯(lián)
URL分享:http://chinadenli.net/article32/dhjipc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、App開發(fā)、網(wǎng)站改版、定制網(wǎng)站、品牌網(wǎng)站建設(shè)、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容