如何设置TextView的宽度为wrap_content,但限制在父容器宽度的1/3以内。

11

拥有一个 TextView,它的宽度不应该超过其父容器宽度的1/3。如果它的宽度小于父容器的1/3,则应具有 wrap_content 的行为。它的水平兄弟总是在它旁边开始。

尝试了以下方法,它总是硬性地划分为1/3和2/3,因此如果text1的空间小于1/3,则TextView two将无法紧邻其旁边。

LinearLayout更改为RelativeLayout,然后 android:layout_weight="n" 就不起作用了。

基本上,需要定义宽度为 wrap_content ,并且 maxWidth 不超过1/3。

有什么建议吗?

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="3">

    <TextView 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="true"
        android:ellipsize="end"
    />

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:singleLine="true"
        android:ellipsize="end"
        android:layout_weight="2"
    />
</LinearLayout>
5个回答

4
基本上,需要定义宽度为wrap_content,并且最大宽度不超过1/3。
如果只需要这些,我的建议是放弃Weight方法,动态设置TextView的maxWidth值。
像这样:
```java textView.setMaxWidth((int)(screenWidth / 3)); ```
其中,`screenWidth`是屏幕宽度。
tv.setMaxWidth(((LinearLayout)tv.getParent()).getWidth()/3);

3

我认为每次更新textView时,您都需要动态检查父布局的宽度,类似于以下代码(我使用按钮和编辑文本测试了此代码以更改textView - 可以正常工作):

<LinearLayout
    android:id="@+id/myLayout" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView 
        android:id="@+id/tvA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="end"
    />

    <TextView 
        android:id="@+id/tvB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="end"
    />
</LinearLayout>

代码:

            TextView tvA = (TextView) findViewById(R.id.tvA);
            TextView tvB = (TextView) findViewById(R.id.tvB);
            LinearLayout myLayout = (LinearLayout) findViewById(R.id.myLayout);


            // code to use when the textView is updated
            // possibly button onClickListener?
            tvA.measure(0, 0);
            int textWidth = tvA.getMeasuredWidth();
            myLayout.measure(0,0);
            int layoutWidth = myLayout.getWidth();

            if (textWidth > (layoutWidth / 3)) {
                tvA.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
                tvB.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 2.0f));

            } else {
                tvA.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                tvB.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            }

我不希望它总是1/3,而是最多1/3,它应该允许小于1/3,并且兄弟节点应该紧挨着它而没有空格。 - lannyf
1
谢谢Mark!如果无法在XML中实现,您的答案将是正确的。 - lannyf

2
解决方案很简单:您的第二个TextView的宽度必须像第一个一样是"0dp"。
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="3">

    <TextView 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="true"
        android:ellipsize="end"
    />

    <TextView 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:ellipsize="end"
        android:layout_weight="2"
    />
</LinearLayout>

抱歉,我有android:layout_width="0dp"的复制错误,没有正确地放置。它无法工作。 - lannyf
这应该是被接受的解决方案,因为它不需要通过编程计算宽度。 - AlexB

0

把它放在一个宽度为父级的1/3的布局中怎么样?

<LinearLayout
  .. this is main parent
  android:weightSum="1" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.33"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

它达到了父元素的三分之一,但没有“如果兄弟元素的宽度小于父元素的三分之一,则应向右流动”的要求。 - lannyf

0

通过引入ConstraintLayout,可以像这样实现:

<?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:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:singleLine="true"
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toStartOf="@+id/guideline"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.333"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toEndOf="@+id/textView1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="initial text"/>
</android.support.constraint.ConstraintLayout>

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