Android 在两个堆叠视图旁边显示一个方形图像视图
Android display a square image view next to two stacked views
我正在尝试在标签和文本框旁边显示图像视图。最终结果应如下所示:
图像视图的高度应与 space 标签和文本框占据的高度相同,宽度应与高度相同,因此它是正方形。
我查看了 this 制作宽度和高度相同的图像视图的问题。
我也看过 其他与我类似的问题,但使用 weightSum
和 layout_weight
会使图像视图占用太多 space 或变成扭曲。我也试过结合这两个问题的答案,但没有任何效果。
您可以使用 ConstraintLayout
:
非常简单地实现此目的
<?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="match_parent"
android:background="#f4f4f4"
tools:context="MainActivity">
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.3"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.3"
android:elevation="6dp"
android:layout_margin="8dp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintBottom_toBottomOf="@+id/button2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/button2"
android:background="@color/colorAccent"/>
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:elevation="6dp"
android:layout_marginLeft="8dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView2"
app:layout_constraintTop_toTopOf="@+id/imageView2" />
<EditText
android:id="@+id/editText4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:elevation="6dp"
android:ems="10"
android:layout_marginLeft="8dp"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintBottom_toBottomOf="@+id/imageView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView2" />
</androidx.constraintlayout.widget.ConstraintLayout>
看起来像这样
重要属性:
要使您的图片正方形:
app:layout_constraintDimensionRatio="1:1"
要使您的观点具有响应性:
app:layout_constraintWidth_percent
app:layout_constraintHeight_percent
连同:
android:layout_width="0dp"
android:layout_height="0dp"
- 使您对容器的视图看起来像是在其中(在此示例中,我使用了一个按钮,但您可以随意使用任何类型的视图)
android:elevation="6dp"
我正在尝试在标签和文本框旁边显示图像视图。最终结果应如下所示:
图像视图的高度应与 space 标签和文本框占据的高度相同,宽度应与高度相同,因此它是正方形。
我查看了 this 制作宽度和高度相同的图像视图的问题。
我也看过 weightSum
和 layout_weight
会使图像视图占用太多 space 或变成扭曲。我也试过结合这两个问题的答案,但没有任何效果。
您可以使用 ConstraintLayout
:
<?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="match_parent"
android:background="#f4f4f4"
tools:context="MainActivity">
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.3"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="0.3"
android:elevation="6dp"
android:layout_margin="8dp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintBottom_toBottomOf="@+id/button2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/button2"
android:background="@color/colorAccent"/>
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:elevation="6dp"
android:layout_marginLeft="8dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView2"
app:layout_constraintTop_toTopOf="@+id/imageView2" />
<EditText
android:id="@+id/editText4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:elevation="6dp"
android:ems="10"
android:layout_marginLeft="8dp"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintBottom_toBottomOf="@+id/imageView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/imageView2" />
</androidx.constraintlayout.widget.ConstraintLayout>
看起来像这样
重要属性:
要使您的图片正方形:
app:layout_constraintDimensionRatio="1:1"
要使您的观点具有响应性:
app:layout_constraintWidth_percent
app:layout_constraintHeight_percent
连同:
android:layout_width="0dp"
android:layout_height="0dp"
- 使您对容器的视图看起来像是在其中(在此示例中,我使用了一个按钮,但您可以随意使用任何类型的视图)
android:elevation="6dp"