我有以下代码,其中有 4 行并排的 2 个 TextView :
<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>
它显示以下内容:
如何修改代码以使其始终显示 50%/50% TextView 中没有什么文本?
最佳答案
将 TextView
的 width
都更改为 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>
在水平
LinearLayout
中使用weight
时,您需要width
为0dp
。同样,在 vertical
LinearLayout
中,您需要 height
为 0dp
。
关于java - 如何并排调整两个 TextView 的大小并使用屏幕宽度的 50%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19407909/