【问题标题】:Margin auto calculated in LinearLayout在 LinearLayout 中自动计算边距
【发布时间】:2020-01-23 17:31:25
【问题描述】:

我有问题。 我想实现这个:

我正在使用线性布局。 LinearLayout 和 ChildViews 的大小是 const。问题是,我的工作结果看起来是这样的:

这是我的代码:

<LinearLayout
    android:id="@+id/answersLayout"
    android:weightSum="3"
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    android:orientation="horizontal"
    android:background="@drawable/bck_number_red"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/boxOfRecycleViewer" >

    <FrameLayout
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@color/colorOperator"/>

    <FrameLayout
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@color/colorOperator"/>


    <FrameLayout
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@color/colorOperator"/>

</LinearLayout>

【问题讨论】:

    标签: android android-layout android-linearlayout


    【解决方案1】:

    您似乎正在使用ConstraintLayout 来包含您的LinearLayout。使用ConstraintLayout 应该很容易做到这一点。

    如果您使用LinearLayout 来提供背景,那么您可以使用Space 在视图之间添加权重。

    <Space
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="1" />
    

    【讨论】:

      【解决方案2】:

      可以通过ConstrainLayout打包三种布局并设置横链样式来实现,如下:

      <?xml version="1.0" encoding="utf-8"?>
      <androidx.constraintlayout.widget.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="50dp"
          tools:context=".MainActivity">
      
          <FrameLayout
              android:id="@+id/firstContainer"
              android:layout_width="50dp"
              android:layout_height="50dp"
              android:background="@color/colorOperator"
              app:layout_constraintEnd_toStartOf="@+id/secondContainer"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent"
              app:layout_constraintHorizontal_chainStyle="spread_inside" />
      
          <FrameLayout
              android:id="@+id/secondContainer"
              android:layout_width="50dp"
              android:layout_height="50dp"
              android:background="@color/colorOperator"
              app:layout_constraintEnd_toStartOf="@id/thirdContainer"
              app:layout_constraintStart_toEndOf="@+id/firstContainer"
              app:layout_constraintTop_toTopOf="parent" />
      
      
          <FrameLayout
              android:id="@+id/thirdContainer"
              android:layout_width="50dp"
              android:layout_height="50dp"
              android:background="@color/colorOperator"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toEndOf="@+id/secondContainer"
              app:layout_constraintTop_toTopOf="parent" />
      
      </androidx.constraintlayout.widget.ConstraintLayout>
      

      【讨论】:

        【解决方案3】:

        我认为最好的解决方案是在中间放置 5 个Views:3 个FrameLayouts(你的 3 个FrameLayouts)和 2 个白色的Views。这样:

        <LinearLayout
            android:id="@+id/answersLayout"
            android:weightSum="3"    <!-- This is useless, if you don't define weight parameter in each child -->
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:orientation="horizontal"
            android:background="@drawable/bck_number_red"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/boxOfRecycleViewer" >
        
            <FrameLayout
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:background="@color/colorOperator"/>
        
            <View
                android:layout_width="100dp"
                android:layout_height="50dp"
                android:background="#FFF"/>
        
            <FrameLayout
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:background="@color/colorOperator"/>
        
            <View
                android:layout_width="100dp"
                android:layout_height="50dp"
                android:background="#FFF"/>
        
            <FrameLayout
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:background="@color/colorOperator"/>
        
        </LinearLayout>
        

        但我认为使用权重更好,并且允许布局根据设备进行扩展:

        <LinearLayout
            android:id="@+id/answersLayout"
            android:weightSum="3.5"    <!-- Now it has significance -->
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:orientation="horizontal"
            android:background="@drawable/bck_number_red"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/boxOfRecycleViewer" >
        
            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="0.5"
                android:background="@color/colorOperator"/>
        
            <View
                android:layout_width="0dp"
                android:layout_height="100dp"
                android:layout_weight="1"
                android:background="#FFF"/>
        
            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="0.5"
                android:background="@color/colorOperator"/>
        
            <View
                android:layout_width="0dp"
                android:layout_height="100dp"
                android:layout_weight="1"
                android:background="#FFF"/>
        
            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="0.5"
                android:background="@color/colorOperator"/>
        
        </LinearLayout>
        

        最后,我建议你使用ConstraintLayout

        【讨论】:

          猜你喜欢
          • 2014-08-09
          • 1970-01-01
          • 2021-12-31
          • 2013-11-01
          • 2018-08-10
          • 2011-12-30
          • 2014-06-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多