Android XML表格布局?两个相同宽度的行填充相同宽度的按钮?
以下是我的XML for LAND格式的一部分:Android XML表格布局?两个相同宽度的行填充相同宽度的按钮?,android,xml,width,android-tablelayout,Android,Xml,Width,Android Tablelayout,以下是我的XML for LAND格式的一部分: <TableLayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center" android:stretchColumns="*"> <TableRow> <Button android:id="@
<TableLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:stretchColumns="*">
<TableRow>
<Button
android:id="@+id/countbutton"
android:text="@string/plus1"/>
<Button
android:id="@+id/resetbutton"
android:text="@string/reset"
/>
</TableRow>
</TableLayout>
现在我不知道的是,一行的宽度和按钮的宽度取决于按钮内的文本。如果这两个文本相等,就可以说:文本是OK -表的一半在屏幕的中间。但是如果他们有不同的大小-让我们说“A”和“这是长按钮”,表的中心不再在屏幕中间,所以按钮不是相等的宽度… 有按钮的行,其中按钮的大小与你需要做的一样。
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="0dip"/>
<Button android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="0dip"/>
</LinearLayout>
并填写按钮的其他xml属性
神奇之处在于布局的重量和宽度属性。您不需要表格布局。这些属性告诉布局您的视图应该在父布局中占据相等的空间。除了接受的答案之外: 我有一个类似的问题,我需要在一个网格中有几个相同列宽的图像,所以我使用了表布局。它可以工作,但是当异步加载图像时,相应的列将占据整个宽度,直到所有列中至少有一个图像为止 我用Robby Pond的解决方案解决了这个问题,但最后一行不起作用,因为最后一行的图像不一定像其他行那样多,当我想让这些图像放在上面相同的列中时,会拉伸这些图像以占用所有可用空间。为了解决这个问题,我用常规视图对象填充了该行剩余的空列 使用与所有其他图像相同的布局参数:
宽度=0,重量=1。
这就解决了问题 布局片段
<TableLayout
android:id="@+id/tablelayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
以编程方式设置表中按钮布局属性的代码:
public void addButtons(View view) {
TableLayout tableLayout = (TableLayout) findViewById(R.id.tablelayout);
Context context = getApplicationContext();
tableLayout.removeAllViews();
for (int rowIndex = 0; rowIndex < ROWS; rowIndex++) {
TableRow row = new TableRow(context);
for (int columnIndex = 0; columnIndex < COLUMNS; columnIndex++) {
Button btn = new Button(context);
LayoutParams buttonParams = new LayoutParams(0,
LayoutParams.WRAP_CONTENT, 1f);
btn.setLayoutParams(buttonParams);
row.addView(btn);
}
tableLayout.addView(row);
}
}
公共无效添加按钮(视图){
TableLayout TableLayout=(TableLayout)findViewById(R.id.TableLayout);
Context=getApplicationContext();
tableLayout.removeallview();
对于(int-rowIndex=0;rowIndex
很好的示例(源于)
测试和工作:
<TableRow>
<!-- Column 1 -->
<TextView
android:id="@+id/tbl_txt1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:background="@color/red"
android:textColor="@color/white"
android:padding="10dip"
android:layout_margin="4dip"
android:layout_weight="1"
android:text="Column 1" />
<!-- Column 2 -->
<TextView
android:id="@+id/tbl_txt2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:background="@color/red"
android:textColor="@color/white"
android:padding="10dip"
android:layout_margin="4dip"
android:layout_weight="1"
android:text="Column 2" />
<!-- Column 3 -->
<TextView
android:id="@+id/tbl_txt3"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:background="@color/red"
android:textColor="@color/white"
android:padding="10dip"
android:layout_margin="4dip"
android:layout_weight="1"
android:text="Column 3" />
</TableRow>
尝试以下方法:测试并运行:
<TableRow>
<!-- Column 1 -->
<TextView
android:id="@+id/tbl_txt1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:background="@color/red"
android:textColor="@color/white"
android:padding="10dip"
android:layout_margin="4dip"
android:layout_weight="1"
android:text="Column 1" />
<!-- Column 2 -->
<TextView
android:id="@+id/tbl_txt2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:background="@color/red"
android:textColor="@color/white"
android:padding="10dip"
android:layout_margin="4dip"
android:layout_weight="1"
android:text="Column 2" />
<!-- Column 3 -->
<TextView
android:id="@+id/tbl_txt3"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:background="@color/red"
android:textColor="@color/white"
android:padding="10dip"
android:layout_margin="4dip"
android:layout_weight="1"
android:text="Column 3" />
</TableRow>
1) 对于tablelayout设置android:stretchColumns=“1”
2) 同时设置每个项目(列)layout\u width=“0dip”和layout\u weight=“1”
3) 并且不要设置表格行的布局宽度
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="inext.smartshop.CustomerProfile.CustomerProfileView">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tablelayout"
android:stretchColumns="1"
android:layout_above="@+id/userProfilebtnsignout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="@+id/relativeLayout">
<TableRow
android:layout_height="fill_parent"
android:padding="5dp"
android:id="@+id/detailsTableRow"
android:gravity="center_horizontal">
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Full Name : "
android:layout_width="0dip"
android:layout_weight="1"
android:id="@+id/textView8"
android:gravity="right" />
<TextView
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Ram Chhabra"
android:id="@+id/userProfilename"
android:gravity="left" />
</TableRow>
<TableRow android:padding="5dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Email Address : "
android:id="@+id/textView10"
android:gravity="right" />
<TextView
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="RAMCHHABRA0012@GMAIL.COM"
android:id="@+id/userProfileemail"
android:gravity="left"
/>
</TableRow>
<TableRow android:padding="5dp">
<TextView
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Contact No 1 : "
android:id="@+id/textView12"
android:gravity="right" />
<TextView
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="8130032232"
android:id="@+id/userProfilecontactone"
/>
</TableRow>
</TableLayout>
</RelativeLayout>
</RelativeLayout>
首先,您需要将android:stretchColumns=“*”添加到TableLayout 每个表格行需要有
android:layout\u weight=“1”
,以便均匀地拉伸
TableRow中的每个按钮都需要有android:layout\u width=“0dp”
这里有一些例子
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TableRow
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:onClick="onButtonClick"
android:text="@string/button1" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:onClick="onButtonClick"
android:text="@string/button2" />
</TableRow>
<TableRow
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:onClick="onButtonClick"
android:text="@string/button3" />
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:onClick="onButtonClick"
android:text="@string/button4" />
</TableRow>
</TableLayout>
如何在代码中为动态创建的按钮设置这些值?parant.addView(newView,newlinearlayout.LayoutParams(0/*width*/,WRAP\u CONTENT,1/*weight*/);如果您是通过搜索找到的,请注意,您需要在LinearLayout上执行android:layout\u width=“fill\u parent”或其他值。我在运行时遇到了关于如何指定布局宽度的错误,我花了一段时间才发现它是在抱怨LinearLayout,而不是包含的具有零值布局宽度的项。另请参见。如何使两排高度相等?请看我的问题,这是表格布局+1的完美答案