我正在尝试在卡片视图右侧的版式中放置一个“ New”标签:
我没有在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"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
app:cardElevation="2dp"
app:cardCornerRadius="4dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginStart="10dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:contentDescription="@string/img_desc"
android:src="@mipmap/ic_launcher"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/img"
android:layout_centerVertical="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@color/cardview_head"
android:textSize="16sp"
android:textStyle="bold"
tools:text="test" />
<TextView
android:id="@+id/labelNew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/new_label"
android:ellipsize="end"
android:maxLines="1"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:text="@string/new_label"
android:textColor="@color/white"
android:textSize="12sp"
tools:text="New" />
</LinearLayout>
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@color/cardview_subhead"
tools:text="test" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
尝试这种方式