【问题标题】:Is there a better way to do this layout ? Maybe using Constraint Layout?有没有更好的方法来做这个布局?也许使用约束布局?
【发布时间】:2016-10-13 18:29:47
【问题描述】:

有没有更简单(更好)的方式来做这个布局?也许使用 ConstraintLayout?

3333 必须始终位于右下角,2222 必须始终位于 1111 下方。

不确定是否有更好的方法来使用单个 RelativeLayout 或 FrameLayout?

我已经尝试过约束布局,但 Android Studio 在移动元素时会有点疯狂。

  <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true">

<LinearLayout
    android:id="@+id/LeftLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_centerVertical="true"
    android:layout_margin="10dp"
    android:layout_toStartOf="@+id/textView3"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="2"
        android:text="11111111111111111111111111111111111111111111111111111111" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLines="1"
        android:text="222222222"
        android:visibility="visible" />

</LinearLayout>

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="3333"
    android:layout_alignBottom="@+id/LeftLayout"
    android:layout_alignParentEnd="true" />       
</RelativeLayout>

【问题讨论】:

    标签: android android-layout android-constraintlayout


    【解决方案1】:

    使用ConstraintLayout,您可以创建包含“111”的文本视图,其宽度设置为MATCH_CONSTRAINT (0dp),高度设置为maxLines 和WRAP_CONTENT。然后将其约束到父级的左侧和顶部,并在右侧约束到包含“3333”的视图。带有“222”的视图被简单地垂直限制在“111”的底部,并且在父视图的左侧。 “333”然后简单地被限制在父级的底部和右侧。

    <?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"
        android:layout_width="match_parent"
        android:padding="8dp"
        android:layout_height="wrap_content">
    
        <TextView
            android:id="@+id/textView7"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="1111111111111111111111111111111111111111111111111111111111"
            android:maxLines="2"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintRight_toLeftOf="@+id/textView8" />
    
        <TextView
            android:id="@+id/textView9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2222222222"
            app:layout_constraintTop_toBottomOf="@+id/textView7"
            app:layout_constraintLeft_toLeftOf="parent" />
    
        <TextView
            android:id="@+id/textView8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3333"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="parent" />
    
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

      【解决方案2】:

      对于这类场景,您可以使用带权重的 LinearLayout。

      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal">
      
          <LinearLayout
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:orientation="vertical">
      
              <TextView
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center_horizontal"
                  android:text="11111111111111111111111111111111111111111" />
      
              <TextView
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center_horizontal"
                  android:text="2222222222" />
          </LinearLayout>
      
          <TextView
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:gravity="bottom" 
              android:text="3333" />
      </LinearLayout>
      

      【讨论】:

      • 所以总共只有3个TextView(不是4个),3333也需要在右下角。所以你需要将 android:gravity="bottom" 和 android:layout_height="match_parent" 添加到 3333 textview 中。如果约束布局提供更简单的东西,我的问题更多。
      • 是的,你是对的,这似乎是推荐的,我从Android Nanodegree程序中学习的。
      猜你喜欢
      • 2021-11-15
      • 1970-01-01
      • 2018-11-04
      • 2022-12-03
      • 2014-05-26
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多