代码之家  ›  专栏  ›  技术社区  ›  Alan

如何将CardView宽度设置为嵌套的TextView宽度

  •  -1
  • Alan  · 技术社区  · 6 年前

    我创造了一个 CardView 小部件,其中 TextView 是。的文本 文本视图 运行时中的更改。我怎么能绑住我的 卡地维 宽度到 文本视图 宽度?

    到目前为止我所做的:

    卡地维 ,我补充说, android:layout_alignLeft="@id/textViewUser" 但它不能作为 文本视图 不能使用此方法嵌套。有什么想法吗?

    一小段 卡地维 项目如下。请注意,我删除了限制,使它更短。

    <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>
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   blackr4y    6 年前

    如果我理解正确,您需要保持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>
    
    推荐文章