代码之家  ›  专栏  ›  技术社区  ›  CL So

如何确定LinearLayout项目的位置?

  •  -1
  • CL So  · 技术社区  · 7 年前

    我创建了一个包含5个文本视图的水平线性布局

    我设置的每个文本视图 android:layout_weight="1"

    线性布局为 android:layout_width="match_parent"

    当我运行应用程序时,它们平均分布

    如果在运行时将较长的文本设置为TextView元素之一,则另一个TextView将更改位置

    我可以修复所有位置吗?

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="20"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="20"/>
    
        <TextView
            android:id="@+id/change_value"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:onClick="changeValue"
            android:text="20" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="20"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="20"/>
    </LinearLayout>
    
    
    
    public void changeValue(View v){
        TextView tv=(TextView)v;
        tv.setText("zzzzzzzzzzzz");
    }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Poovarasan Selvaraj    7 年前

    试试这个,

     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:text="first"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <TextView
            android:text="first"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <TextView
            android:text="first"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <TextView
            android:text="first"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <TextView
            android:text="first"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
    </LinearLayout>
    

    正在某些文本字段上添加更多文本

    enter image description here

        2
  •  0
  •   Amir Mohsenian    7 年前

    设置以下代码,而不是layout\u width:

    android:layout_width="0dip"