【问题标题】:Android set max width in percentage of a TextView in LinearLayout of vertical orientationAndroid在垂直方向的LinearLayout中以TextView的百分比设置最大宽度
【发布时间】:2017-07-17 16:32:43
【问题描述】:

我想将TextViewlayout_weighttv_long_text 设置为以下LinearLayoutvertical 的80% > 方向。

<LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical">
    <TextView
            android:id="@+id/tv_short_text"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            tools:text="short text" />
    <TextView
            android:id="@+id/tv_long_text"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="0.8"
            tools:text="a pretty long text" />
</LinearLayout>

上述方法不起作用,因为 textview 父级的方向是垂直的。

所以,我尝试在 xml 中设置 android:layout_width="match_parent",然后 通过获取测量的宽度在运行时设置宽度,然后将宽度设置为 80%,但 getMeasuredWidth 是给我0。

int measuredWidth = longTextView.getMeasuredWidth();
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) longTextView.getLayoutParams();
params.width = (int) (measuredWidth * 0.8);
longTextView.setLayoutParams(params);

我也尝试在运行时设置 layout_weight,但它也不起作用,可能是因为它的父视图是垂直方向的。

longTextView.setLayoutParams(
        new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.MATCH_PARENT,
                0.8f)
);

对我有用的是为长文本视图添加一些额外视图。但是为了尝试以百分比设置此视图的宽度,又添加了 2 个额外的视图。 还有其他有效的方法吗?

<LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical">
    <TextView
            android:id="@+id/tv_short_text"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            tools:text="short text" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/tv_long_text"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="0.8"
            android:textStyle="bold"
            tools:text="a pretty long text" />
        <View
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"/>
    </LinearLayout>
</LinearLayout>

【问题讨论】:

    标签: android android-linearlayout android-layout-weight


    【解决方案1】:

    我不会试图摆弄运行时间测量。您的第二个水平布局解决方案非常好,因为您有两个水平扩展的 TextView。
    另一个选项是支持库中的PercentRelativeLayout com.android.support:percent:25.1.0 See here

    这是来自文档

     <android.support.percent.PercentRelativeLayout
             xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:app="http://schemas.android.com/apk/res-auto"
             android:layout_width="match_parent"
             android:layout_height="match_parent">
         <ImageView
             app:layout_widthPercent="50%"
             app:layout_heightPercent="50%"
             app:layout_marginTopPercent="25%"
             app:layout_marginLeftPercent="25%"/>
     </android.support.percent.PercentRelativeLayout>
    

    【讨论】:

    • 现在在 2021 年迁移到 AndroidX,需要替换为 androidx.percentlayout.widget.PercentRelativeLayout
    【解决方案2】:

    使用 android:layout_weight

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                />
            <android.support.v4.widget.Space
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="3"/>
        </LinearLayout>
    

    尝试这种结构让 TextView 最多只能流动到屏幕宽度的 75%。 您可以调整 SPACER 宽度以实现您想要的 2 为 50%。

    【讨论】:

      【解决方案3】:

      您可以使用android:weightSum,并且无需额外的View 即可获得相同的结果:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:tools="http://schemas.android.com/tools"
                  android:id="@+id/activity_main"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:weightSum="10"
                  android:orientation="vertical">
      
          <TextView
              android:id="@+id/tv_short_text"
              android:layout_height="wrap_content"
              android:layout_width="wrap_content"
              tools:text="short text" />
          <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:weightSum="10">
              <TextView
                  android:id="@+id/tv_long_text"
                  android:layout_height="wrap_content"
                  android:layout_width="0dp"
                  android:layout_weight="8"
                  android:textStyle="bold"
                  tools:text="a pretty long text a pretty long text a pretty long text" />
          </LinearLayout>
      </LinearLayout>
      

      【讨论】:

      • 仍然有这个额外的 LinearLayout 包装器:P
      • LinearLayout 右侧的包装保留,因为使用了 80% 的空间,所以必须有一些东西,但 TextView 是 80%,当您显示一些文本时,只有该区域会被使用:)
      猜你喜欢
      • 1970-01-01
      • 2011-12-18
      • 2012-02-05
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      • 2011-04-11
      • 2016-04-01
      • 2017-07-28
      相关资源
      最近更新 更多