简体   繁体   English

如何并排放置两个textview并使用50%宽度的屏幕

[英]How to size two textview side by side and use 50% width of screen

I have the following code which has 2 textviews side by side in 4 rows: 我有以下代码,它有4行并排的2个文本视图:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    android:layout_weight="1.1"
    android:gravity="center"
    android:weightSum="4" >
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center" >
        <TextView
            android:id="@+id/tvCodeNameDetail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="cnsdsdsf:"
            android:layout_weight="1"
            android:gravity="center"
            android:background="#ff0000" />
        <TextView
            android:id="@+id/tvCodeName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:layout_weight="1"
            android:gravity="center"
            android:background="#00FF00" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center" >
        <TextView
            android:id="@+id/tvVersionDetail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="vnddf: "
            android:layout_weight="1"
            android:gravity="center"
            android:background="#00ff00" />
        <TextView
            android:id="@+id/tvVersion"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:layout_weight="1"
            android:gravity="center"
            android:background="#ff0000" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center" >
        <TextView
            android:id="@+id/tvReleaseDetail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="rdsdfsdfsfsdf: "
            android:layout_weight="1"
            android:gravity="center"
            android:background="#FF0000" />
        <TextView
            android:id="@+id/tvRelease"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:layout_weight="1"
            android:gravity="center"
            android:background="#00FF00" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center" >
        <TextView
            android:id="@+id/tvAPIDetail"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="apdd: "
            android:layout_weight="1"
            android:gravity="center"
            android:background="#00ff00" />
        <TextView
            android:id="@+id/tvAPI"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:layout_weight="1"
            android:gravity="center"
            android:background="#ff0000" />
    </LinearLayout>
</LinearLayout>

It displays the following: 它显示以下内容:

在此输入图像描述

How do I modify the code to make it ALWAYS 50%/50% no what text is inside the textview? 如何修改代码使其总是50%/ 50%没有文本视图中的文本?

Change the width of your TextView s both to 0dp instead of wrap_content TextViewwidth更改为0dp而不是wrap_content

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="center" >
    <TextView
        android:id="@+id/tvCodeNameDetail"
        android:layout_width="0dp"              <!-- here -->
        android:layout_height="wrap_content"
        android:text="cnsdsdsf:"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#ff0000" />
    <TextView
        android:id="@+id/tvCodeName"
        android:layout_width="0dp"              <!-- and here -->
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#00FF00" />
</LinearLayout>

When using weight in a horizontal LinearLayout you need to have a width of 0dp . horizontal LinearLayout使用weight ,您需要具有0dpwidth Likewise, in a vertical LinearLayout , you would want a height of 0dp . 同样,在vertical LinearLayout ,您需要height0dp

The code worked for me, but I substracted both of "0dp" arguments and instead used only "weight=1". 代码对我有用,但我减去了两个“0dp”参数,而只使用了“weight = 1”。 I am using a custom row maker for a list view and it worked :D 我使用自定义行生成器列表视图,它工作:D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM