如何设置 CardView 宽度等于嵌套 TextView 宽度
How to set CardView width equals to nested TextView width
我创建了一个 CardView
小部件,其中有一个 TextView
。 TextView
的文本在运行时发生变化。如何将 CardView
宽度绑定到 TextView
宽度?
目前我尝试过的:
在 CardView
中,我添加了 android:layout_alignLeft="@id/textViewUser"
但它不起作用,因为无法使用此方法嵌套 TextView
。有什么想法吗?
CardView
项目的片段如下。请注意,我删除了限制以使其更短。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="@+id/cardViewUser"
android:layout_width="300dp"
android:layout_height="30dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
card_view:cardCornerRadius="4dp"
card_view:elevation="14dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="I change during runtime"
android:id="@+id/textViewUser"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
如果我对你的理解是正确的,你想保持 CardView 的宽度与 TextView 相连。您可以尝试这样的操作:
<androidx.cardview.widget.CardView
android:id="@+id/cardViewUser"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
card_view:cardCornerRadius="4dp"
card_view:elevation="14dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="I change during runtime"
android:id="@+id/textViewUser"/>
</androidx.cardview.widget.CardView>
我创建了一个 CardView
小部件,其中有一个 TextView
。 TextView
的文本在运行时发生变化。如何将 CardView
宽度绑定到 TextView
宽度?
目前我尝试过的:
在 CardView
中,我添加了 android:layout_alignLeft="@id/textViewUser"
但它不起作用,因为无法使用此方法嵌套 TextView
。有什么想法吗?
CardView
项目的片段如下。请注意,我删除了限制以使其更短。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="@+id/cardViewUser"
android:layout_width="300dp"
android:layout_height="30dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
card_view:cardCornerRadius="4dp"
card_view:elevation="14dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="I change during runtime"
android:id="@+id/textViewUser"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
如果我对你的理解是正确的,你想保持 CardView 的宽度与 TextView 相连。您可以尝试这样的操作:
<androidx.cardview.widget.CardView
android:id="@+id/cardViewUser"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
card_view:cardCornerRadius="4dp"
card_view:elevation="14dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="I change during runtime"
android:id="@+id/textViewUser"/>
</androidx.cardview.widget.CardView>