【问题标题】:Textviews equal in height in Android ScrollViewAndroid ScrollView中的Textviews高度相等
【发布时间】:2017-04-26 04:51:50
【问题描述】:

我有一个带有 3 个 TextView 的 Android ScrollView:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    xmlns:android="http://schemas.android.com/apk/res/android">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/text1"
                android:layout_weight="1"
                android:text="short text 1"
                android:textSize="30dp"
                android:padding="16dp"
                android:layout_width="match_parent"
                android:layout_height="0dp" />
            <TextView
                android:id="@+id/text2"
                android:layout_weight="1"
                android:text="Very long text Very long text Very long text Very long text"
                android:textSize="50dp"
                android:padding="16dp"
                android:layout_width="match_parent"
                android:layout_height="0dp" />
            <TextView
                android:id="@+id/text3"
                android:layout_weight="1"
                android:text="short text 2"
                android:textSize="30dp"
                android:padding="16dp"
                android:layout_width="match_parent"
                android:layout_height="0dp" />
        </LinearLayout>
</ScrollView>

我想要以下行为:

  • 所有 TextView 的高度都相同 - 有效
  • 如果文本没有填满屏幕,TextViews 应该填满整个屏幕(因此 fillViewport="true") - 有效
  • 如果文本比屏幕大,ScrollView 开始滚动,而且 TextView 的高度仍然相等(所有 TextView 的高度等于 TextView 的最高值) - 不起作用:TextView 被剪裁,ScrollView 不滚动。

这是屏幕的样子(TextView2 被剪裁了)

【问题讨论】:

  • 当 textview2 的文本很长时,您希望 textviews 如何表现?
  • 我希望 textViews2 的文本被完全看到,并且所有 textViews 应该具有相同的长度。所以 textView1 和 textView3 应该适应 textView2 的高度。由于整个布局会高于屏幕,我希望 ScrollView 滚动。
  • android:layout_height="wrap_content" 放入滚动视图
  • 那么 textView2 仍然被裁剪。它否定了 fillViewport="true" 的效果
  • @Damnum 这个问题的定义是错误的。因为如果你想要与weight 相同的高度,那么当第二个文本大小很大时,显然它会被剪裁。

标签: android android-layout textview android-scrollview


【解决方案1】:

你应该使用android:weightSum="3" 在线性布局中。在TextView中基于weightsum的linearLayout分为三个相等的部分,如果textview使用android:layout_weight="1"

【讨论】:

  • 不改变结果。正如您在屏幕截图中看到的那样,分成 3 个相等的部分已经可以了,但是 textView2 被剪裁并且屏幕不会滚动。
  • TextPaint 油漆 = txtview2.getPaint(); int wordwidth = (int) paint.measureText("a", 0, 1); int screenwidth = getResources().getDisplayMetrics().widthPixels; int num = 屏幕宽度/字宽; if (!expand) { expand = true; ObjectAnimator 动画 = ObjectAnimator.ofInt(desc_txt, "maxLines", num);动画.setDuration(100).start();以下代码有助于将 textview 的高度提高为文本长度。
【解决方案2】:

你可以试试

 android:layout_height="wrap_content"

在文本视图中并删除

android:layout_weight="1"

      <TextView
            android:id="@+id/text1"
            android:text="short text 1"
            android:textSize="30dp"
            android:padding="16dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-28
    • 2023-03-09
    • 2014-07-08
    • 1970-01-01
    相关资源
    最近更新 更多