在Android的TableLayout按键宽度宽度、按键、Android、TableLayout

2023-09-06 02:50:37 作者:你若不惜,我亦不爱。

我在底部的一个按钮一个TableLayout。我的问题是,按钮拉伸以填充柱的整个宽度,即使我不希望它的宽。

I have a TableLayout with a button at the bottom. My problem is that the button stretches to fill the entire width of the column even though I don't want it that wide.

中的XML看起来是这样的:

The XML looks like this:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:stretchColumns="*"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
   <TableRow android:layout_height="wrap_content"
        android:layout_width="fill_parent"> 
     <CheckBox android:id="@+id/check1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" 
      android:text="Some text 1"/>
     <CheckBox android:id="@+id/check2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Some text 1"/>
   </TableRow>
   <TableRow android:layout_height="wrap_content"
        android:layout_width="fill_parent">  
     <CheckBox android:id="@+id/check3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" 
      android:text="Even some more text 2"/>
     <CheckBox android:id="@+id/check4"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Even some more text 2"/>
   </TableRow> 
   <TableRow android:layout_height="wrap_content"
        android:layout_width="fill_parent"> 
     <CheckBox android:id="@+id/check5"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" 
      android:text="An even longer line of text 3"/>
     <CheckBox android:id="@+id/check6"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Another longer line of text 3"/> 
   </TableRow>

   <EditText
      android:id="@+id/myEditText1"
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"
      android:inputType="textMultiLine"
      android:singleLine="false"
      android:text="Enter some text"
    />

   <TableRow> 
   <Button android:id="@+id/SaveButton"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:text="Save">
  </Button> 
  </TableRow>       

</TableLayout>

即使我设定一个明确的宽度为它忽略它的按钮。举例来说,如果我有:

Even if I set an explicit width to the button it ignores it. For example if I have:

   <Button android:id="@+id/SaveButton"
      android:layout_width="100sp"
      android:layout_height="100sp"
      android:text="Save">
  </Button> 

...按钮获取更高,但宽度还是拉伸以填充列。

... the button gets taller but the width still stretches to fill the column.

我想按钮,大约一半的当前宽度,中心在列。 在此先感谢!!

I'd like the button to be about half its current width, centered in the column. Thanks in advance!!

推荐答案

@mbaird是正确的:安卓stretchColumns =*总是要延伸的宽度列。但是,你仍然可以得到你想要的列内行为,通过把一个的FrameLayout成绵延列的宽度的列,然后把你的按钮在里面。然后你就可以在按钮的大小,只要你喜欢和的FrameLayout内的中心是:

@mbaird is right: android:stretchColumns="*" is always going to stretch the width of the column. However, you can still get the behavior you want inside the column by putting a FrameLayout into the column which stretches the width of the column, and then put your button inside there. Then you can make the size of the button however you like and center it within the FrameLayout:

<TableRow>
    <FrameLayout
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/SaveButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:paddingLeft="40dp"
            android:paddingRight="40dp"
            android:text="Save">
        </Button>
    </FrameLayout>
</TableRow>
 
精彩推荐