<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 1. 前言 對于滑動菜單,其實也就是常見的側滑。 # 2. 實現 它是一個布局,在布局中允許放入兩個直接子控件:第一個子控件是主屏幕中顯示的內容,第二個子控件是滑動菜單中顯示的內容。 ~~~ <?xml version="1.0" encoding="utf-8"?> <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <!--使用FAB,需要使用androidx.coordinatorlayout.widget.CoordinatorLayout包裹起來--> <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!--引入下拉刷新控件--> <androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:id="@+id/swiperefreshLayout" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- 將內容包裹起來 --> <TextView android:layout_width="wrap_content" android:layout_height="?attr/actionBarSize" android:text="測試下拉刷新" /> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout> <com.google.android.material.floatingactionbutton.FloatingActionButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_baseline_add_24" app:elevation="8dp" android:layout_gravity="bottom|end" android:layout_margin="16dp" /> </androidx.coordinatorlayout.widget.CoordinatorLayout> <!--側滑菜單內容--> <TextView android:layout_width="match_parent" android:background="@color/black" android:layout_height="match_parent" android:layout_gravity="start" android:text="@string/app_name" android:textColor="@color/white" /> </androidx.drawerlayout.widget.DrawerLayout> ~~~ `layout_gravity`這個屬性是必須指定的,因為我們需要告訴`DrawerLayout`滑動菜單是在屏幕的左邊還是右邊,指定`left`表示滑動菜單在左邊,指定`right`表示滑動菜單在右邊。這里我指定了`start`,表示會根據系統語言進行判斷,如果系統語言是從左往右的,比如英語、漢語,滑動菜單就在左邊,如果系統語言是從右往左的,比如阿拉伯語,滑動菜單就在右邊。 側滑后效果: ![](https://img.kancloud.cn/42/7e/427e0f0978e255ed8cafbf4e4f4c62f0_324x492.png) # 3. 加入導航按鈕 很多用戶可能根本就不知道有這個功能,所以可以在`Toolbar`的最左邊加入一個導航按鈕,點擊按鈕也會將滑動菜單的內容展示出來。那么,這里需要將`Toolbar`加入到這個布局文件中: ~~~ <?xml version="1.0" encoding="utf-8"?> <androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <!--使用FAB,需要使用androidx.coordinatorlayout.widget.CoordinatorLayout包裹起來--> <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 將內容包裹起來 --> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/teal_200"></androidx.appcompat.widget.Toolbar> <!--引入下拉刷新控件--> <androidx.swiperefreshlayout.widget.SwipeRefreshLayout android:id="@+id/swiperefreshLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="?attr/actionBarSize"> <!--下拉刷新的內容--> <TextView android:layout_width="wrap_content" android:layout_height="?attr/actionBarSize" android:text="測試下拉刷新" /> </androidx.swiperefreshlayout.widget.SwipeRefreshLayout> <!--FAB懸浮按鈕--> <com.google.android.material.floatingactionbutton.FloatingActionButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="16dp" android:src="@drawable/ic_baseline_add_24" app:elevation="8dp" /> </androidx.coordinatorlayout.widget.CoordinatorLayout> <!--側滑菜單內容--> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="start" android:background="@color/black" android:text="@string/app_name" android:textColor="@color/white" /> </androidx.drawerlayout.widget.DrawerLayout> ~~~ 此時效果: ![](https://img.kancloud.cn/03/f0/03f08cd6bb82fbbb5bca60c0d63c4415_277x424.png) 然后,我們需要在`Toolbar`的左邊加入一個菜單圖標。對應的代碼: ~~~ class TestActivity : AppCompatActivity() { // 找到控件 val swiperefreshLayout by lazy { findViewById<SwipeRefreshLayout>(R.id.swiperefreshLayout) } val toolbar by lazy { findViewById<Toolbar>(R.id.toolbar) } val drawerlayout by lazy { findViewById<DrawerLayout>(R.id.drawerlayout) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_test) // 設置下拉刷新的圓圈的顏色 swiperefreshLayout.setColorSchemeResources(R.color.purple_700) // 模擬數據耗時請求 swiperefreshLayout.setOnRefreshListener { refershData() } // 設置Toolbar setSupportActionBar(toolbar) supportActionBar?.setDisplayHomeAsUpEnabled(true) toolbar.setNavigationIcon(R.drawable.ic_baseline_menu_24) toolbar.setNavigationOnClickListener { // 打開側滑 drawerlayout.openDrawer(GravityCompat.START) } } fun refershData() { // 模擬請求,耗時兩秒結束 thread { Thread.sleep(2000) runOnUiThread { // 設置停止下拉刷新 swiperefreshLayout.isRefreshing = false } } } } ~~~ 效果: ![](https://img.kancloud.cn/bd/a2/bda2732c2a816199495ec35efb3119fe_396x613.png) 點擊左邊的菜單按鈕可以顯示側滑菜單。當然,對于事件的監聽還是可以使用`Toolbar`中介紹的: ~~~ override fun onOptionsItemSelected(item: MenuItem): Boolean { // item.itemId } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看