如何使用带有ConstraintLayout的DrawerLayout

时间:2017-02-07 19:09:31

标签: android drawerlayout android-constraintlayout

我让public enum MetricType : Int { case mvps = 0 case allNBA = 1 case championshipRings = 2 case finalAppearances = 3 case gamesPlayed = 4 case ppg = 5 static func count() -> Int { return (ppg.rawValue) + 1 } static var allValues: [MetricType] { var array: [MetricType] = Array() var item : MetricType = MetricType.mvps while item.rawValue < MetricType.count() { array.append(item) item = MetricType(rawValue: (item.rawValue + 1))! } return array } 的DrawerLayout工作正常,但现在我想把它移植到android.support.design.widget.NavigationView。这不是很好,想要求建议吗?

原始工作XML:

ConstraintLayout

PORTING TO ConstraintLayout :(不工作抽屉陈旧,无法刷卡进/出)

<android.support.v4.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:id="@+id/drawer" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context="com.port.android.ui.ActivityMain"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include android:id="@+id/toolbar" layout="@layout/tool_bar" /> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/frame" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> </FrameLayout> </LinearLayout> <android.support.design.widget.NavigationView android:id="@+id/navigation_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:headerLayout="@layout/navigation_view_header" app:menu="@menu/menu_navigation"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:orientation="horizontal" android:padding="20dp"> <Button android:id="@+id/btn_exit" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="exitAddress" android:text="Exit Address" /> </LinearLayout> </android.support.design.widget.NavigationView> </android.support.v4.widget.DrawerLayout> 是我插入Frags

的地方
"@+id/frame"

2 个答案:

答案 0 :(得分:0)

您可以将按钮和框架布局小部件组合为约束布局而不是线性布局。但是,应该使用线性布局来定义NavHostFragment,并且可以将NavigationView单独定义为DrawerLayout的最后一个子项。注意:这里LinearLayout和ConstraintLayout是抽屉布局的2个子代。线性布局保存NavHostFragment,ConstraintLayout保存工具栏,在FrameLayout中定义的小部件以及“退出”按钮。抽屉布局将是第三个子项。

答案 1 :(得分:0)

你可以试试这个..

<?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout     
        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">
    
    <android.support.v4.widget.DrawerLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">    
    </android.support.v4.widget.DrawerLayout>
    
    </android.support.constraint.ConstraintLayout>