2012-04-10 71 views
1

我有一个自定义对话框有3个按钮,有时它只有一个按钮。当我有3个按钮时,LinearLayout本身就适合这些按钮。但是,当我只有一个按钮时,它将整个宽度提供给一个按钮,使该按钮看起来太大。我想,如果只有一个按钮,它应该只需要一半的整个宽度可以或者应该换行的内容(按钮图像。)请参见下面的图像的参比LinearLayout中的按钮问题

3 buttons dialog

1 button dialog

+0

请发表您的源 – 2012-04-10 07:49:57

+0

我觉得杰森问:请张贴的XML为你的布局,使我们可以看看,并回答意味深长 – 2012-04-10 08:04:44

+3

我不认为这个问题值得它得到了downvote。这是一个非常有效和清晰的问题。是的,在指出错误时可能需要XML,但我认为即使没有当前的布局实现,也可以给出几个很好的答案。请不要过于乐意与你的赞誉,人民! – dbm 2012-04-10 08:14:09

回答

3

请参阅此XML文件,该文件与您的要求类似。只是知道去了不需要的按钮。

<?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" 
    android:orientation="horizontal" android:weightSum="3" android:gravity="center_horizontal"> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Button"/> 


    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="Button"/> 

</LinearLayout> 
+0

谢谢。有效。 – Rajkiran 2012-04-10 09:16:25

+0

另外;如果你想让所有的按钮具有相同的宽度,你应该在所有按钮上设置“android:layout_width”属性为“0dp”。正如@Agarwal给出的例子,你的按钮在包装内容时会首先得到一个宽度(“联系人”按钮将比“添加”按钮变宽),然后剩余宽度均匀分布在三个按钮之间。如果它们都以零宽度开始,则在最终大小重新分配之后它们将具有完全相同的宽度。我真的以为我看到一个解释这个问题的答案,但显然我的眼睛在欺骗我...... – dbm 2012-04-10 11:46:05

1

在为了有一个按钮,最多一半需要

  1. 集机器人可用的屏幕宽度:weightSum =父“2”的LinearLayout
  2. 集android:layout_weight =“1”中的按钮