Я создал виджет 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>
@Treewallie большое спасибо за упоминание об этом, я думал так же
Если я вас правильно понимаю, вы хотите сохранить ширину 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>
Как насчет того, чтобы установить
android:layout_width = "wrap_content"
вCardView
? Это помогает?