【问题标题】:How set height layout dependig text length?如何设置高度布局取决于文本长度?
【发布时间】:2013-11-24 18:06:27
【问题描述】:

我有 Scrollview、Layout 和 TextView。

 <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/Layout"
              android:orientation="vertical"
              android:background="@null"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:layout_gravity="left|center_vertical"/>
    </LinearLayout>
    </ScrollView>

如何根据文本长度设置高度布局?如果文本很短,布局高度应该等于屏幕高度。但是当文本很长(例如超过 100 句)时,布局高度应该大于屏幕高度,并且应该取决于文本的长度。

【问题讨论】:

    标签: android android-layout height


    【解决方案1】:

    android:layout_height="wrap_content" 放到您的布局中,这样应该会相应地对其进行缩放。

    【讨论】:

      【解决方案2】:

      android:fillViewport="true" 添加到ScrollView,并设置子android:layout_height="wrap_content"。 (引用自ScrollView's handy trick

      <ScrollView
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/scrollView1"
          android:layout_width="match_parent"
          android:layout_height="match_parent" 
          android:fillViewport="true" >
          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/Layout"
                android:orientation="vertical"
                android:background="@null"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
          <TextView
              android:id="@+id/textView"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="New Text"
              android:layout_gravity="left|center_vertical"/>
          </LinearLayout>
      </ScrollView>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-03
        • 2013-11-21
        • 2016-01-23
        • 1970-01-01
        • 2015-01-15
        相关资源
        最近更新 更多