2011-11-14 41 views
0

在线性布局内我的Actvity中有三个按钮。 在这里,我希望所有的按钮应该占据整个tablelayour同样。 所以我编码如下。表格行中三个按钮的对齐方式Android

<Linearlayout> 
...... 
<TableLayout 
     android:id="@+id/tableLayout2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:stretchColumns="*"> 

     <TableRow 
      android:id="@+id/tableRow5" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 
      <Button 
       android:id="@+id/registerTwitter" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Register Twitter" 
       android:gravity="left"/> 

      <Button 
       android:id="@+id/registerFB" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Register FaceBook" 
       android:gravity="center"/> 

      <Button 
       android:id="@+id/clearCredentials" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Clear Credentials" 
       android:layout_gravity="right" /> 

     </TableRow> 
    </TableLayout> 
    </LinearLayout> 

但是在这里,当我给代码如上我有三个按钮占据整个空间。

但对齐不正确。他们就像在漫天飞舞....

在哪里我犯的错误...

This is the part of output i got

还要注意上面的最后一个按键是不可见的充分的半隐半现.....

我在UI设计非常虚弱任何人都可以在建议....

+0

有点难以明白你是想有什么用。也许画一个你想要的图像,让我们看看它。 – prolink007

+0

@ prolink007 Craigy说我需要将所有按钮放在同一行。没有每个在空中飞行。 – shanmugamgsn

回答

1

重力和layout_gravity是按钮上的两个不同的字段。

重力改变文本的位置在按钮和layout_gravity改变的按钮布局位置

您可以选择使用layout_gravity,另外两个采用重力一个按钮。 确保您使用正确的!

编辑,这里试试这个代码,它是更好地使用权:

<TableRow 
    android:id="@+id/tableRow5" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <Button 
     android:id="@+id/registerTwitter" 
     android:layout_height="wrap_content" 
     android:text="Register Twitter" 
     android:layout_weight=".3" android:layout_width="match_parent" android:gravity="center_horizontal"/> 

    <Button 
     android:id="@+id/registerFB" 
     android:layout_height="wrap_content" 
     android:text="Register FaceBook" 
     android:layout_weight=".3" android:layout_width="match_parent" android:gravity="center_horizontal"/> 

    <Button 
     android:id="@+id/clearCredentials" 
     android:layout_height="wrap_content" 
     android:text="Clear Credentials" 
     android:layout_gravity="right" android:layout_weight=".3" android:layout_width="match_parent" android:gravity="center_horizontal"/> 

</TableRow> 

+0

会完成我的目标吗?因为我已经在我的代码中使用过,但我无法得到它..反正我会尝试你的代码,并得到返回 – shanmugamgsn

+0

那么,.3只是一个建议。由于您有3个按钮,因此很容易将其视为百分比。我怎么做,但你可以使用任何数字。 – MrZander

+0

MrZander这是给只是连续三个按钮..但它并没有占用从父母均匀的整个空间 – shanmugamgsn

0

如果我理解正确的话,你只是想在一行中给出的按钮等于水平空间,然后尝试小号omething这样

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    ... other views here ... 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" > 

     <Button 
      android:id="@+id/registerTwitter" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="Register Twitter" /> 

     <Button 
      android:id="@+id/registerFB" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="Register FaceBook" /> 

     <Button 
      android:id="@+id/clearCredentials" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="Clear Credentials" /> 
    </LinearLayout> 

</LinearLayout> 

这告诉按钮与宽度0开始出并平分了在它们之间的其余的水平空间。

+0

Craigy我会尽力回复哥们.. – shanmugamgsn

+0

Craigy这只是一个线性布局,并不占据整个空间。,,,, – shanmugamgsn

+0

哎呀,你是对的。我已将包含按钮的LinearLayout的'layout_width'更改为'fill_parent'。现在也改变了按钮的高度为“fill_parent” – Craigy

0

你混合gravitylayout_gravity。你应该使用layout_gravity。此外,请注意,center值意味着水平中心和垂直中心。这可以解释为什么你的中间按钮垂直关闭。

+0

所以你的意思是我应该使用Layout_Gravity?如果我使用这样的按钮不填充整个空间,即使我给fill_parent – shanmugamgsn

0

您可以通过在RelativeLayout的,而不是执行的LinearLayout如下

<RelativeLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/button1" 
    android:layout_alignParentLeft="true" 
    android:text="Button1" 

    /> 
<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/button2" 
    android:layout_centerHorizontal="true" 
    android:text="Button2" 
    /> 
    <Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/button3" 
    android:layout_alignParentRight="true" 
    android:text="Button3" 
    /> 

</RelativeLayout> 
+0

deepa这是行不通的,这collapex父线性布局以及 – shanmugamgsn

+0

只需在tablelayout中替换此relativelayout而不是linearlayout。你能解释一下你得到的更多吗? – deepa