同等间距的固定尺寸按钮Android

时间:2011-05-28 11:44:48

标签: java android xml button

我有这个xml文档。我希望这些93 x 93px按钮图像的间距相等。目前,重量设置使它们伸展并相互拥抱,因此没有空间。但是,如果我将重量设置为0,它们会相同地相互拥抱,但会填满大约一半的屏幕。

顺便说一下,这是横向的。

    <LinearLayout android:id="@+id/LinearLayout" android:layout_height="wrap_content"
  android:layout_width="fill_parent">
   <Button 
    android:background="@drawable/a" 
    android:layout_height="93px" 
    android:id="@+id/a"
    android:layout_width="93px" 
    android:layout_weight="1" 
    android:layout_marginLeft="20dip" 
    android:layout_marginTop="30dip"/>
  <Button 
    android:background="@drawable/b" 
    android:layout_height="93px" 
    android:id="@+id/b"
    android:layout_width="93px" 
    android:layout_weight="1"  
    android:layout_marginTop="30dip"/>

  <Button 
    android:background="@drawable/c" 
    android:layout_height="93px" 
    android:id="@+id/c" 
    android:layout_weight="1" 
    android:layout_width="93px"  
    android:layout_marginTop="30dip"/>
  <Button 
    android:background="@drawable/d" 
    android:layout_height="93px" 
    android:id="@+id/d"
    android:layout_width="93px" 
    android:layout_weight="1" 
    android:layout_marginRight="20dip" 
    android:layout_marginTop="30dip" />
    </LinearLayout>

5 个答案:

答案 0 :(得分:4)

解决方案很简单。

您必须将每个按钮保持为单独的线性布局,并为每个线性布局保持重量= 1。现在按钮的间距相等,无论分辨率或方向如何。

EX: -

      <LinearLayout android:id="@+id/LinearLayout" android:layout_height="wrap_content"
      android:layout_width="fill_parent">
      <LinearLayout android:id=@+id/Button1Parent" android:layout_height="wrap_content" android_width="wrap_content" android:weight="1" gravity="center">  
       <Button 
        android:background="@drawable/a" 
        android:layout_height="93px" 
        android:id="@+id/takequiz"
        android:layout_width="93px" 
        android:layout_weight="1" 
        android:layout_marginLeft="20dip" 
        android:layout_marginTop="30dip"/>
</LinearLayout>
<LinearLayout android:id=@+id/Button2Parent" android:layout_height="wrap_content" android_width="wrap_content" android:weight="1" gravity="center">
      <Button 
        android:background="@drawable/b" 
        android:layout_height="93px" 
        android:id="@+id/random"
        android:layout_width="93px" 
        android:layout_weight="1"  
        android:layout_marginTop="30dip"/>
</LinearLayout>
<LinearLayout android:id=@+id/Button3Parent" android:layout_height="wrap_content" android_width="wrap_content" android:weight="1" gravity="center">

      <Button 
        android:background="@drawable/c" 
        android:layout_height="93px" 
        android:id="@+id/search" 
        android:layout_weight="1" 
        android:layout_width="93px"  
        android:layout_marginTop="30dip"/>
</LinearLayout>
<LinearLayout android:id=@+id/Button4Parent" android:layout_height="wrap_content" android_width="wrap_content" android:weight="1" gravity="center">

      <Button 
        android:background="@drawable/d" 
        android:layout_height="93px" 
        android:id="@+id/browse"
        android:layout_width="93px" 
        android:layout_weight="1" 
        android:layout_marginRight="20dip" 
        android:layout_marginTop="30dip" />
        </LinearLayout>
</LinearLayout>

答案 1 :(得分:2)

试试这个:

<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:background="#000000"
        android:id="@+id/linearLayout01"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="horizontal"
    >
        <Button
            android:layout_width="0dp"
            android:id="@+id/btn1"
            android:layout_height="93px"
            android:layout_weight="1"
            android:layout_marginRight="5dp"
        ></Button>
        <Button
            android:layout_width="0dp"
            android:id="@+id/btn2"
            android:layout_height="93px"
            android:layout_weight="1"
            android:layout_marginRight="5dp"
        ></Button>
        <Button
            android:layout_width="0dp"
            android:id="@+id/btn3"
            android:layout_height="93px"
            android:layout_weight="1"
            android:layout_marginRight="5dp"
        ></Button>
        <Button
            android:layout_width="0dp"
            android:id="@+id/btn4"
            android:layout_height="93px"
            android:layout_weight="1"
        ></Button>
    </LinearLayout>

答案 2 :(得分:1)

LinearLayout没有“等间距”的内在概念。您可以考虑使用为“仪表板”设计的布局,例如https://gist.github.com/882650

答案 3 :(得分:0)

我尝试将当前的LinearLayout更新为以下内容并且具有方形均匀间距的按钮 - 但它们不适合我的模拟器屏幕。所以在我的情况下,你在左边和右边设置的边距正在压缩它们。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/LinearLayout" 
   android:layout_height ="wrap_content"
   android:layout_width  ="fill_parent" 
   android:orientation   ="horizontal"
   android:layout_gravity="center_horizontal">

   <Button  
    android:id="@+id/takequiz"  
    android:text="take quiz"
    android:layout_height="93px"
    android:layout_width ="93px" 
    android:layout_marginTop ="30dip"/>

  <Button 
    android:id="@+id/random" 
    android:text="random"
    android:layout_height="93px" 
    android:layout_width ="93px" 
    android:layout_marginTop="30dip"/>

  <Button 
    android:id="@+id/search" 
    android:text="search"
    android:layout_height="93px"
    android:layout_width="93px" 
    android:layout_marginTop="30dip"/>

  <Button
    android:id="@+id/browse"
    android:text="browse"
    android:layout_height="93px" 
    android:layout_width="93px" 
    android:layout_marginTop="30dip" />

 </LinearLayout>

你考虑过2行吗?我使用TableLayout为声卡类型的应用程序做了类似的事情。虽然这些天我倾向于使用RelativeLayout更多。这是表格代码。

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/LinearLayout" 
   android:layout_height ="fill_parent"
   android:layout_width  ="fill_parent" >
   <TableRow
      android:layout_height ="wrap_content"
      android:layout_width  ="fill_parent" 
      android:gravity       ="center">    
       <Button  
        android:id  ="@+id/takequiz"  
        android:text="take quiz"
        android:layout_height="93px"
        android:layout_width ="93px" />    
      <Button 
        android:id  ="@+id/random" 
        android:text="random"
        android:layout_height="93px" 
        android:layout_width ="93px" />
    </TableRow> 
    <TableRow
      android:layout_height ="wrap_content"
      android:layout_width  ="fill_parent" 
      android:gravity       ="center">    
        <Button 
          android:id  ="@+id/search" 
          android:text="search"
          android:layout_height="93px"
          android:layout_width ="93px" />       
        <Button
          android:id  ="@+id/browse"
          android:text="browse"
          android:layout_height="93px" 
          android:layout_width ="93px"  />
    </TableRow>
 </TableLayout>

答案 4 :(得分:0)

只需将左边距或右边距设置为任意一个按钮

相关问题