如何解决如何约束需要wrap_content但内容可能大于显示的Android视图?
假设我要显示任意文本,并且由于应用程序需求和其余布局细节,我不能将其放在ScrollView或类似内容中-文本可以是任意长度,必须在一行上,并且如果太大,只能在屏幕上显示所有内容的唯一方法是根据显示尺寸缩小比例。它需要使用wrap_content来设置宽度和高度,因为它的宽度可能会小于显示范围,因此在这种情况下我们不想对其进行拉伸。
为解决此问题,我首先尝试将TextView放在ConstraintLayout中,并限制其宽度和高度。我希望它可以自动将TextView缩小(如果它太大),以适应其宽度/高度作为match_parent给出的父级的约束。不幸的是,它没有明显的效果,并且文本愉快地在屏幕外显示。接下来,我尝试手动缩放TextView以使其适合我的应用程序代码,但是当我在TextView上调用getMeasuredWidth / Height时,我总是会得到显示尺寸(大概是因为测量值限制在显示范围之内)。
布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black">
<TextView
android:id="@+id/text"
android:text="teeeeeeeeeeeeeeesteeeeeeeeeeeeeeeeeeeeeeesteeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeesteeeeeeeeeeeeeeeeeeeest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:textSize="300sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constrainedWidth="true"
app:layout_constrainedHeight="true"
android:maxLines="1"
/>
</android.support.constraint.ConstraintLayout>
衡量报告代码:
View textView = findViewById(R.id.text);
ViewTreeObserver textViewTreeObserver = textView.getViewTreeObserver();
textViewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Log.i("MyActivity","oncreate; text view dimens are "+textView.getMeasuredWidth()+","+textView.getMeasuredHeight());
}
});
日志告诉我“文本视图的暗度为2094,1017”,这是显示屏幕的暗度减去系统视图,并且文本在屏幕上显示的内容过多之后才被截断(我在屏幕上看到的只是“ teee “)。
注意
TextView示例只是更大问题的征兆;我可能需要处理对于显示来说太大的运行时图像等,因此我正在寻找一种方法来检测并按比例缩小(或理想情况下设置ConstraintLayout来为我这样做),以便在视图。想到的唯一选择(考虑到View的测量值显然会限制显示边界的事实)是在View介入之前找出问题,例如通过查看原始图像的变暗或我们收到的TextView与TextView当前字体缩放比例的文本数据,并根据需要进行修改,然后将其设置为View的内容。麻烦在于它与内容类型密切相关,并且我希望避免在View子类理论上在布局过程中的某个时刻已经拥有信息时就编写大量代码来管理运行时内容。
问题:
- 为什么约束解决方案没有按this answer自动将TextView缩小以适合ConstraintLayout的match_parent宽度和高度?
- 即使该值大于设备的实际显示边界,如何获得显示视图内容所需的总像素大小?
解决方法
...
onChange={(event) => {
row.pressTypeCode = event.target.value;
}}
设置太大的字体会导致结果,您可以参考autosizing-textview了解如何处理这种情况。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。