android - 在 CardView 中设置 TextView 而不调整视图大小
问题描述
我正在为一个应用程序的设置页面工作,用户需要根据他们的偏好设置文本大小。我正在预览 a 中的一段示例文本,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: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>
希望它能解决你的问题。
推荐阅读
- mysql - 如何在 SQL 中按记录类型获取最新记录
- angularjs - angularjs:http.post() 成功执行,但没有向服务器文件发布任何内容
- tinymce - Tinymce - 如何让用户知道快捷方式
- node.js - 如何使用另一个集合 moongoose 查找子查询文档
- python - 如果值为 X,Python 删除行
- ios - 使用 Airpods 录制立体声音频
- cors - CORS - 无法确定何时何地
- javascript - 在给定的时间间隔内尽可能多次调用函数
- probability - 贝叶斯网络的概率计算
- xamarin - 默认情况下不切换时,开关控制会出现褪色