【发布时间】:2021-08-09 09:50:53
【问题描述】:
目前,我正在尝试为我的应用程序创建 UI,并遇到了 Amazon Android 应用程序的设计。我对以下内容非常感兴趣:
我现在的问题是:这个 UI 元素由什么组成?我会想到一个卡片视图中有两个卡片视图。这是我目前的做法:
<com.google.android.material.card.MaterialCardView
android:id="@+id/cvAddress"
android:layout_width="0dp"
android:layout_height="100dp"
app:strokeColor="@color/color_user_address_border"
app:strokeWidth="1dp"
app:cardCornerRadius="8dp"
app:layout_constraintEnd_toStartOf="@+id/margin_right"
app:layout_constraintStart_toStartOf="@+id/margin_left"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="50dp"
app:strokeWidth="0dp"
app:cardCornerRadius="0dp" />
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="50dp"
app:strokeWidth="0dp"
app:cardCornerRadius="0dp" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
非常接近,但我的边框更厚,看起来有点丑。
编辑:完成的 XML 布局
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/cvAddress"
android:layout_width="0dp"
android:layout_height="152dp"
android:layout_marginTop="8dp"
android:background="@drawable/rounded_background_address"
app:layout_constraintEnd_toStartOf="@+id/margin_right"
app:layout_constraintStart_toStartOf="@+id/margin_left"
app:layout_constraintTop_toBottomOf="@+id/tvUserDataAddressHeadline">
<TextView
android:id="@+id/tvAddAddress"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:gravity="center_vertical"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:text="Add new Address"
android:textColor="@color/color_text_dark"
android:textSize="16sp"
app:drawableEndCompat="@drawable/ic_arrow_big"
app:drawableTint="@color/color_text_dark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/view2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/color_divider"
app:layout_constraintBottom_toBottomOf="@+id/tvAddAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvAddAddress" />
<TextView
android:id="@+id/tvShippingAddress"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:gravity="center_vertical"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:text="Shipping Addresses"
android:textColor="@color/color_text_dark"
android:textSize="16sp"
app:drawableEndCompat="@drawable/ic_arrow_big"
app:drawableTint="@color/color_text_dark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvAddAddress" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/color_divider"
app:layout_constraintBottom_toBottomOf="@+id/tvShippingAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvShippingAddress" />
<TextView
android:id="@+id/tvBillingAddress"
android:layout_width="0dp"
android:layout_height="50dp"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:gravity="center_vertical"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:text="Billing Addresses"
android:textColor="@color/color_text_dark"
android:textSize="16sp"
app:drawableEndCompat="@drawable/ic_arrow_big"
app:drawableTint="@color/color_text_dark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvShippingAddress" />
</androidx.constraintlayout.widget.ConstraintLayout>
图片
点击第一项
点击第二个项目
点击第三项
【问题讨论】:
-
这只是一个简单的视图组,带有自定义可绘制对象和带有
drawableEnd的TextView,因为没有高程,所以不需要使用CardView。 -
你不需要在那里做太多嵌套......要么使用内部带有 TextViews 的单个垂直线性布局(使用 drawableEnd),要么使用 ConstraintLayout 并添加 Textviews(以及可选的 imageViews 作为好吧,如果您需要对图像进行更多控制)。如果需要,您可以使用 cardView,但 CardView 的唯一子项应该是 1 个 ConstraintLayout 或 1 个 LinearLayout...不需要/不需要其他 viewGroup。
-
你的困惑让我有点困惑(看看我做了什么?)。每个小部件/视图,无论它是什么,都可以是“可点击的”并具有您所称的“动画”(波纹)。
ViewGroup(ConstraintLayout、LinearLayout 等)只是“可以分组更多视图的视图”,但它们本身也是视图,因此,如果您愿意,也可以“点击” .话虽如此,如果您希望每个项目都是可点击的,则需要像通常那样为每个项目分配一个 Click Listener,而不管它们具有哪个级别的嵌套。这对你有意义吗? -
我相信
clickable默认情况下也是如此,或者至少如果您分配了一个点击侦听器,则不需要添加它。可聚焦很好,特别是如果您正在处理辅助功能并希望“屏幕阅读器”能够聚焦项目(通过 TalkBack 阅读其contentDescription)。 -
将点击监听器(和前景)分配给您在那里的 ConstraintLayouts,而不是整个 cardView。
标签: android android-xml material-components-android