gpt4 book ai didi

android - 如何获得具有 2 个等宽列的 TableLayout

转载 作者:太空宇宙 更新时间:2023-11-03 11:28:57 26 4
gpt4 key购买 nike

为什么我的表格的列宽不相等?这是完整的布局:首先是表格,然后是表格外的 LinearLayout 中的两个按钮,它们被平分。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp" >

<TableRow
android:layout_marginTop="2dp"
android:gravity="center_vertical" >

<TextView
style="@style/Label.Plain"
android:layout_width="match_parent"
android:text="@string/Caption" />

<EditText
android:background="#00ff00"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="@dimen/button_height"
android:inputType="numberDecimal"
android:selectAllOnFocus="true"
android:textSize="@dimen/spinner_text" />
</TableRow>
</TableLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="3dp"
android:gravity="center_vertical"
android:orientation="horizontal" >

<Button
android:id="@+id/btnOk"
style="@style/Button.Plain.Fill"
android:layout_weight="1"
android:text="@string/ALS_OK" />

<Button
android:id="@+id/btnClose"
style="@style/Button.Plain.Fill"
android:layout_weight="1"
android:text="@string/CANCEL" />
</LinearLayout>
</LinearLayout>

</ScrollView>

下面是它的外观(为了可见性而绘制的一列):

enter image description here

我可以在这里使用线性布局,只是想了解TableLayout背后的逻辑。

最佳答案

你可以通过不同的方式获得它,

喜欢用

android:stretchColumns="*" 

在 TableLayout 或

android:weightSum="1" 

在 TableRow 和

 android:layout_width="0dp" 
android:layout_height="wrap_content"

在 TableRow 和任何 View 中。

这是一个例子

<?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">
<TableLayout
android:id="@+id/tblTop10List"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_marginTop="23dp"
android:stretchColumns="*" >

<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1" >

<TextView
android:id="@+id/tvTopName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:text="HELLO THERE"
android:textColor="#FFF000"
android:layout_column="0"
/>

<TextView
android:id="@+id/tvTopNumber"
android:paddingLeft="4dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#FFF000"
android:text="HELLO THERE "
android:layout_column="1"
/>
</TableRow>
</TableLayout>

</LinearLayout>

** 有关表格布局及其属性的更多信息,请查看这篇不错的文章

http://www.informit.com/articles/article.aspx?p=2007353&seqNum=7

关于android - 如何获得具有 2 个等宽列的 TableLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16213801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com