在垂直方向的LinearLayout中,如何将TextView的最大宽度设置为百分比?(针对Android技术)

9

我想要在以下垂直排列的LinearLayout中,将含有tv_long_textTextViewlayout_weight 设置为 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>
4个回答

6
我不会尝试玩弄运行时测量。你的解决方案使用第二个水平布局是完全可以的,因为你有两个TextView在水平方向上扩展。
另一种选项是从支持库 com.android.support:percent:25.1.0 使用PercentRelativeLayout查看这里

这是来自文档的内容。

 <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。 - djdance

3
使用 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%。

1
你可以使用 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 - s-hunter
LinearLayout在右侧保持了包裹,因为使用了80%的空间,但是TextView只有80%,当显示一些文本时,只有那个区域会被使用 :) - Aleksandar G

0

使用 ConstraintLayout 的解决方案:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/selector_rect_white_gray">

    <TextView
        android:id="@+id/tv_short_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="short text" />

    <TextView
        android:id="@+id/tv_long_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_short_text"
        app:layout_constraintWidth_percent=".8"
        tools:text="very long text - 80% of screen width exact" />

</androidx.constraintlayout.widget.ConstraintLayout>

very long text - 80% of screen width exact


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接