【问题标题】:Set TextView inside CardView without resizing views在 CardView 中设置 TextView 而不调整视图大小
【发布时间】:2019-11-02 02:14:20
【问题描述】:

我正在开发一个应用程序的设置页面,用户需要根据自己的喜好设置文本大小。我正在预览cardView 中的一段示例文本,以便他们可以查看并应用他们的更改。 我的问题是,随着文本的增加或减少,卡片视图会自行调整。如何防止 cardView 在文本更改时推送其他视图?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:background="@android:color/white"
    android:layout_height="match_parent"
    <androidx.cardview.widget.CardView
        android:id="@+id/previewView"
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        card_view:cardElevation="3dp"
        card_view:cardUseCompatPadding="true"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/previewText"
            android:text="My Text"
            android:fontFamily="@font/muli_bold"
            android:layout_gravity="center"
            android:textSize="29sp"
            android:gravity="center"
            android:textColor="@color/customTextColor"
            android:padding="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </androidx.cardview.widget.CardView>
    <ScrollView
        android:layout_below="@+id/previewView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
     //other views here
        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

【问题讨论】:

  • 给定高度值?
  • 是的。这样其他视图不受影响

标签: android textview cardview


【解决方案1】:

如果我正确理解了这个问题,您只需要像这样为卡片设置一个恒定的高度:android:layout_height="200dp"。 所以,不管里面的文字有多大,卡片会保持不变,但文字会被截掉。

这是完整的 CardView 代码:

 <androidx.cardview.widget.CardView
    android:id="@+id/previewView"
    android:layout_margin="10dp"
    android:layout_width="match_parent"
    card_view:cardElevation="3dp"
    card_view:cardUseCompatPadding="true"
    android:layout_height="200dp">
    <TextView
        android:id="@+id/previewText"
        android:text="My Text"
        android:fontFamily="@font/muli_bold"
        android:layout_gravity="center"
        android:textSize="29sp"
        android:gravity="center"
        android:textColor="@color/customTextColor"
        android:padding="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</androidx.cardview.widget.CardView>

希望它能解决您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 2017-06-21
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    相关资源
    最近更新 更多