简体   繁体   English

Android TableLayout中的按钮宽度

[英]Button width in Android TableLayout

I have a TableLayout with a button at the bottom. 我有一个TableLayout,底部有一个按钮。 My problem is that the button stretches to fill the entire width of the column even though I don't want it that wide. 我的问题是按钮伸展以填充列的整个宽度,即使我不希望它那么宽。

替代文字

The XML looks like this: XML看起来像这样:

<?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 is right: android:stretchColumns="*" is always going to stretch the width of the column. @mbaird是对的: android:stretchColumns="*"总是会拉伸列的宽度。 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. 但是,您仍然可以通过将FrameLayout放入拉伸列宽度的列中,然后将按钮放在其中来获得列中所需的行为。 Then you can make the size of the button however you like and center it within the FrameLayout: 然后,您可以根据需要设置按钮的大小,并将其置于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>

I'm pretty sure android:stretchColumns="*" is going to always cause the button to be stretched like that. 我很确定android:stretchColumns="*"总会导致按钮被拉伸。 Have you considered placing the button outside the TableLayout? 您是否考虑过将该按钮放在TableLayout外面?

From the documentation: The children of a TableLayout cannot specify the layout_width attribute. 从文档中:TableLayout的子项不能指定layout_width属性。 Width is always MATCH_PARENT. 宽度始终为MATCH_PARENT。 However, the layout_height attribute can be defined by a child; 但是,layout_height属性可以由子项定义; default value is WRAP_CONTENT 默认值为WRAP_CONTENT

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

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