代码之家  ›  专栏  ›  技术社区  ›  Tapesh Gupta

相对于另一个文本视图空间展开一个文本视图空间

  •  -1
  • Tapesh Gupta  · 技术社区  · 8 年前
    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="2">
    
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:textSize="24sp"
                android:text="Name."
                android:textColor="#000"
                android:textStyle="bold"
                android:background="#c91799c9"
                android:layout_height="wrap_content" />
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:textSize="17sp"
                android:paddingLeft="8dp"
                android:text="Paragraph"
                android:textColor="#000"
                android:textStyle="bold"
                android:background="#3f1799c9"
                android:layout_height="wrap_content" />
    
        </LinearLayout>
    

    在给定的代码中,我在水平视图中有两个文本视图。 我希望当一个文本视图的文本进入另一行时,另一个文本视图的空间也应该根据第一个文本视图增加

    正如给定的图像一样,段落可以是3-4行,但我希望名称文本视图应该垂直展开

    6 回复  |  直到 8 年前
        1
  •  2
  •   Ratilal Chopda    8 年前

    试试这个

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="2">
    
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:textSize="24sp"
                android:text="Name."
                android:textColor="#000"
                android:textStyle="bold"
                android:background="#c91799c9"
                android:layout_height="match_parent" />
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:textSize="17sp"
                android:paddingLeft="8dp"
                android:text="Paragraph"
                android:textColor="#000"
                android:textStyle="bold"
                android:background="#3f1799c9"
                android:layout_height="match_parent" />
    
        </LinearLayout>
    
    </LinearLayout>
    

    输出

    enter image description here

        2
  •  1
  •   Harshad Prajapati    8 年前

    试试这个, 设置 android:layout_height="match_parent" 在两者中 textView 您还可以设置 Textsize 两者相同 文本视图 android:textSize="24sp"

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="2">
    
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#c91799c9"
        android:text="Name."
        android:textColor="#000"
        android:textSize="24sp"
        android:textStyle="bold" />
    
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#3f1799c9"
        android:paddingLeft="8dp"
        android:text="Paragraph"
        android:textColor="#000"
        android:textSize="17sp"
        android:textStyle="bold" />
    
    </LinearLayout>
    
        3
  •  0
  •   Vidhi Dave    8 年前

    尝试以下操作:

    只要给高度 match\u父项

    enter image description here

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="2">
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#c91799c9"
            android:text="Name."
            android:textColor="#000"
            android:textSize="24sp"
            android:textStyle="bold" />
    
        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#3f1799c9"
            android:paddingLeft="8dp"
            android:text="paragraph"
            android:textColor="#000"
            android:textSize="17sp"
            android:textStyle="bold" />
    
    </LinearLayout>
    

    如果希望文本位于中间,可以添加 android:gravity="center_vertical" 在两个文本视图中。

        4
  •  0
  •   TheHound.developer    8 年前

    将重心放在第一个文本视图上。ie在第一个文本视图“Name”中添加以下代码。

    android:layout_gravity="center"
    

    还可以更改两个TextView的高度以匹配\u parent。

     android:layout_height="match_parent"
    
        5
  •  0
  •   Víctor Calderón    7 年前

    您必须在显示布局后设置宽度,否则您将得到 错误,存在命令。可以用来避免这种情况的onPost 错误

    <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
                <ImageView
                android:id="@+id/my_image"
                android:layout_width="wrap_content"
                android:layout_height="50dp"/>
                <EditText
                android:id="@+id/et_user"
                android:layout_width="wrap_content"
                android:layout_height="50dp"/>
                <Button
                android:id="@+id/bt_start"
                android:layout_width="wrap_content"
                android:layout_height="50dp"/>
    </LinearLayout>
    

    java代码必须放在OnCreate方法中,但必须放在 setContentView

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView my_image = findViewById(R.id.my_image);
    Button bt_start = findViewById(R.id.bt_start);
    EditText et_user = = findViewById(R.id.et_user);
    if ((my_image != null))
        {
            my_image.post(new Runnable()
            {
                @Override
                public void run()
                {
                    bt_start.setWidth(my_image.getWidth());
                    et_user.setWidth(my_image.getWidth());                                 
                }
        });
    }
    

    在我的示例中,可以使用 my_image.getWidth() 并将此值设置为其他组件,如bt\U start或et\U user,但 您可以在任何其他组件中执行此操作。

    my_image != null
    

    可以更改为任何其他布尔比较,如:

    bt_start != null
    et_user != null
    

    这只是为了确认从中获取宽度的项目不为null,因此 您可以正确获取宽度,如果此项为null,则会得到 空点异常。

        6
  •  -1
  •   Ankita    8 年前
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="2"
        xmlns:android="http://schemas.android.com/apk/res/android">
    
       <TextView
           android:layout_width="0dp"
           android:layout_weight="1"
           android:textSize="24sp"
           android:text="Name"
           android:textColor="#000"
           android:textStyle="bold"
           android:gravity="center"
           android:background="#c91799c9"
           android:layout_height="match_parent" />
       <TextView
           android:layout_width="0dp"
           android:layout_weight="1"
           android:textSize="17sp"
           android:paddingLeft="8dp"
           android:text="Paragraph"
           android:gravity="center"
           android:textColor="#000"
           android:textStyle="bold"
           android:background="#3f1799c9"
           android:layout_height="match_parent" />
    
    </LinearLayout>