【问题标题】:Linear layout match parent or weight doesn't work in Coordinator layout线性布局匹配父级或权重在协调器布局中不起作用
【发布时间】:2016-03-26 11:33:17
【问题描述】:
<?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设计中的视图,显示的线性布局确实与父级匹配:

但是输出:

线性布局与父级不匹配,或者我怀疑应用于两个浮动按钮的 layout_weight 在协调器布局中不起作用。

【问题讨论】:

  • 你试过android:layout_gravity="bottom|center_horizontal"
  • @SelçukCihan 刚试过,不起作用:/
  • 线性布局中的android:layout_width="match_parent"也可以改成android:layout_width="wrap_content"吗?
  • @SelçukCihan 谢谢,现在两个晶圆厂位于底部和中间水平。但我想要的视图是将视图分成两半,每个晶圆厂中心水平在每一半。想知道这个问题的原因和解决方法。

标签: android android-linearlayout android-coordinatorlayout android-layout-weight android-layoutparams


【解决方案1】:

这是因为FloatingActionButton只能有一个固定的大小。

您可以在它们之间添加一些空间。

<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>

【讨论】:

    【解决方案2】:

    所以,也许如果你会使用 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>
    

    看起来像这样:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-14
      • 1970-01-01
      • 2019-01-31
      • 2015-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多