【问题标题】:How to mantain uniform alignment in layout如何在布局中保持统一对齐
【发布时间】:2020-12-24 19:31:42
【问题描述】:

我正在尝试找到一种方法来保持文本视图在复杂布局中统一对齐。有没有办法将“1B”TextView 直接定位在“1A”TextView 下方和“1C”TextView 上方,同时避免过多嵌套?

注意:“1B”和“1C”TextViews 一起在 RelativeLayout 中,作为可扩展 RecyclerView 的一部分

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    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:background="@android:color/white">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/linearLayoutA">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/relativeLayoutA">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/ibA"
                android:src="@mipmap/ic_launcher_round"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/tvA"
                android:layout_toEndOf="@+id/ibA"/>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/relativeLayoutB">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/tvB"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/ivC"
                android:src="@mipmap/ic_launcher_round"
                android:layout_below="@id/tvB"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/tvC"
                android:layout_below="@id/tvB"
                android:layout_toEndOf="@id/ivC"/>
        </RelativeLayout>
    </LinearLayout>

</androidx.cardview.widget.CardView>

【问题讨论】:

    标签: android xml android-linearlayout android-percentrelativelayout


    【解决方案1】:

    使用约束布局:

    <?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"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@mipmap/ic_launcher_round" />
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:text="A 1"
            app:layout_constraintStart_toEndOf="@+id/imageView2"
            app:layout_constraintTop_toTopOf="@+id/imageView2" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="B1"
            app:layout_constraintBottom_toTopOf="@+id/imageView4"
            app:layout_constraintEnd_toEndOf="@+id/textView"
            app:layout_constraintStart_toStartOf="@+id/textView"
            app:layout_constraintTop_toBottomOf="@+id/imageView2" />
    
        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="C 1"
            app:layout_constraintEnd_toEndOf="@+id/textView2"
            app:layout_constraintStart_toStartOf="@+id/textView2"
            app:layout_constraintTop_toTopOf="@+id/imageView4" />
    
        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="32dp"
            app:layout_constraintEnd_toEndOf="@+id/imageView2"
            app:layout_constraintStart_toStartOf="@+id/imageView2"
            app:layout_constraintTop_toBottomOf="@+id/imageView2"
            app:srcCompat="@mipmap/ic_launcher_round" />
        </android.support.constraint.ConstraintLayout>
    

    【讨论】:

    • textView2, textView3 & imageView4 需要在 Recyclerview 中展开和隐藏一些布局
    猜你喜欢
    • 1970-01-01
    • 2018-03-08
    • 2016-07-16
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 2018-04-12
    • 1970-01-01
    • 2014-01-14
    相关资源
    最近更新 更多