线性布局匹配父级或权重在协调器布局中不起作用
Linear layout match parent or weight doesn't work in Coordinator layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
tools:context="com.now.sexyfacechanger.Activity_PhotoOrGallery">
<include layout="@layout/content_activity__photo_or_gallery" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_camera"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_camera"
android:layout_weight="1"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_gallery"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_gallery"
android:layout_weight="1"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
这是 android studio design 中的视图,显示的线性布局与父级匹配:
但是输出:
线性布局与父布局不匹配,或者我怀疑应用于两个浮动按钮的 layout_weight 在协调器布局中不起作用。
因为FloatingActionButton
只能有固定大小
你可以在它们之间添加一些space。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal">
<Space
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_camera"/>
<Space
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_gallery"/>
<Space
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
</LinearLayout>
所以,如果您将使用 LinearLayout,也许它可以帮助您。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal"
android:padding="@dimen/space_small_xs"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start"
android:orientation="horizontal"
android:paddingStart="@dimen/space_small_l"
android:paddingEnd="@dimen/space_small_l">
<android.support.design.widget.FloatingActionButton
android:id="@+id/button_prev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow_white_left"
android:tint="@android:color/white"
app:backgroundTint="@color/text_subscribe_light"
app:fabSize="mini" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end"
android:orientation="horizontal"
android:paddingStart="@dimen/space_small_l"
android:paddingEnd="@dimen/space_small_l">
<android.support.design.widget.FloatingActionButton
android:id="@+id/button_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow_white_right"
android:tint="@android:color/white"
app:backgroundTint="@color/text_subscribe_light"
app:fabSize="mini" />
</LinearLayout>
它看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
tools:context="com.now.sexyfacechanger.Activity_PhotoOrGallery">
<include layout="@layout/content_activity__photo_or_gallery" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_camera"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_camera"
android:layout_weight="1"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_gallery"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_gallery"
android:layout_weight="1"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
这是 android studio design 中的视图,显示的线性布局与父级匹配:
但是输出:
线性布局与父布局不匹配,或者我怀疑应用于两个浮动按钮的 layout_weight 在协调器布局中不起作用。
因为FloatingActionButton
只能有固定大小
你可以在它们之间添加一些space。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="horizontal">
<Space
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_camera"/>
<Space
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_gallery"/>
<Space
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
</LinearLayout>
所以,如果您将使用 LinearLayout,也许它可以帮助您。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal"
android:padding="@dimen/space_small_xs"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="start"
android:orientation="horizontal"
android:paddingStart="@dimen/space_small_l"
android:paddingEnd="@dimen/space_small_l">
<android.support.design.widget.FloatingActionButton
android:id="@+id/button_prev"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow_white_left"
android:tint="@android:color/white"
app:backgroundTint="@color/text_subscribe_light"
app:fabSize="mini" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end"
android:orientation="horizontal"
android:paddingStart="@dimen/space_small_l"
android:paddingEnd="@dimen/space_small_l">
<android.support.design.widget.FloatingActionButton
android:id="@+id/button_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow_white_right"
android:tint="@android:color/white"
app:backgroundTint="@color/text_subscribe_light"
app:fabSize="mini" />
</LinearLayout>
它看起来像这样: