【问题标题】:Android make one element take rest of spaceAndroid让一个元素占据剩余空间
【发布时间】:2019-01-19 15:57:16
【问题描述】:

我有一个活动,其中一个元素植根于顶部,另一个元素应该占用剩余的可用空间。第一个元素的内容会随着时间而改变,需要不同的空间,第二个元素需要随之改变大小。这是我已有的简化版本:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:id="@+id/textview_A"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        />

    <TextView
        android:id="@+id/textview_B"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/textview_A"
        app:layout_constraintBottom_toBottomOf="parent"
        />

</android.support.constraint.ConstraintLayout>

目前的方式,顶部元素是正确的,但第二个元素不是。它将自己的位置调整为顶部元素底部和父元素底部的一半,但其高度与其内容相同,而不是所有可用空间。我怎样才能做到这一点?

【问题讨论】:

    标签: android xml android-layout layout


    【解决方案1】:

    设置布局权重 = 1 安卓:布局=“1”

    【讨论】:

      【解决方案2】:

      或者,尝试这样的线性布局:

      <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
              <EditText 
                  android:layout_width="0dp"
                  android:layout_weight="1"
                  android:layout_height="match_parent"
                  android:text="text" />
      
              <ImageView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:src="@mipmap/ic_launcher" />
      
              <Button 
                  android:layout_width="wrap_content"
                  android:layout_height="match_parent"
                  android:text="D" />
          </LinearLayout>
      

      注意这个特殊的 sn-p:

      android:layout_width="0dp"
      android:layout_weight="1"
      

      【讨论】:

        【解决方案3】:

        请在发布此类问题之前进行搜索。 之前关于这个的一些答案


        android:layout_height@id/testview_B 更改为 0dp。类似
        <TextView android:id="@+id/textview_B" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintTop_toBottomOf="@id/textview_A" app:layout_constraintBottom_toBottomOf="parent" />

        【讨论】:

          猜你喜欢
          • 2017-12-18
          • 2011-11-03
          • 1970-01-01
          • 2012-09-27
          • 1970-01-01
          • 1970-01-01
          • 2016-01-08
          • 2013-02-22
          • 1970-01-01
          相关资源
          最近更新 更多