如何设置 textview 宽度 wrap_content 但限制为父宽度的 1/3

how to set textview width wrap_content but limit to 1/3 of parent's width

具有 TextView,其宽度不应超过其父级宽度的 1/3。如果它的宽度小于父级的 1/3,它应该有 wrap_content 行为。它的水平兄弟将始终在它旁边开始。

试过以下,它总是有 1/3 和 2/3 的硬切,所以如果 text1 的 space 少于 1/3,TextView 两个将不会在它旁边开始.

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>

我认为每次更新 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));
            }

解决方案很简单:第二个 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>

如何将它放在父宽度的 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>

basically, need to define width is wrap_content and the maxWidth does not go over 1/3.

如果这就是您所需要的,那么我的建议是放弃权重方法并动态设置 TextView 的 maxWidth 值。

像这样:

tv.setMaxWidth(((LinearLayout)tv.getParent()).getWidth()/3);

通过引入 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>