代码之家  ›  专栏  ›  技术社区  ›  Dennis Vash

如何限制到边距结束?

  •  0
  • Dennis Vash  · 技术社区  · 6 年前

    我想添加一个 arrow icon end 属于 LinearLayout :

    Desired Layout

    最好的方法是什么?

    我通过包装 线性布局 ImageView 具有 ConstraintLayout 但感觉有比使用更优雅的方式 app:layout_constraintHorizontal_bias="1" .

    第一次尝试时 图片框 是其中的一部分 LinerLayout (没有 约束布局 )是否有方法从这里获得所需的布局?

    First Attempt

    电流 .xml :

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.appcompat.widget.LinearLayoutCompat 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:orientation="vertical">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/profile_constraint_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/_16sdp">
    
            <androidx.appcompat.widget.LinearLayoutCompat
                android:id="@+id/profile_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="start"
                android:orientation="horizontal">
    
                <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/imageView"
                    android:layout_marginStart="@dimen/_4sdp"
                    android:layout_width="48dp"
                    android:layout_height="48dp"
                    android:background="@drawable/ic_account_circle_black_48dp"
                    android:contentDescription="@string/nav_bottom_sheet_profile_description" />
    
                <androidx.appcompat.widget.LinearLayoutCompat
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_marginStart="@dimen/_4sdp">
    
                    <androidx.appcompat.widget.AppCompatTextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/jesta_profile_full_name_default"/>
    
                    <androidx.appcompat.widget.AppCompatTextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/jesta_profile_phone_number_default"/>
                </androidx.appcompat.widget.LinearLayoutCompat>
    
            </androidx.appcompat.widget.LinearLayoutCompat>
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="@string/jesta_profile_edit_profile_icon_description"
                android:src="@drawable/ic_keyboard_arrow_right_black_24dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@+id/profile_layout"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintHorizontal_bias="1"/>
        </androidx.constraintlayout.widget.ConstraintLayout>
    
    ...
    </androidx.appcompat.widget.LinearLayoutCompat>
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Dennis Vash    6 年前

    仅约束箭头图标 top , bottom end ,它将自动粘贴到布局的最后。

    因此,不要这样:

    <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="@string/jesta_profile_edit_profile_icon_description"
                android:src="@drawable/ic_keyboard_arrow_right_black_24dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@+id/profile_layout"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintHorizontal_bias="1"/>
    

    有这样的:

        <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"    
                    android:contentDescription="@string/jesta_profile_edit_profile_icon_description"
                    android:src="@drawable/ic_keyboard_arrow_right_black_24dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    />