在Android 3按钮布局的问题

问题描述:

我有一个在布局中有三个按钮的活动。所有按钮都在底部。我想让这个按钮变成极端的左,中间和极右。极右和极左按钮都很好。但是,*按钮占据了左右按钮之间的所有空间。但是,我想要中间的按钮来占用必要的空间而不是所有的空间。另外,我无法将imageButton移动到屏幕中间。任何帮助这些事情?这是我的XML布局文件:在Android 3按钮布局的问题

  <?xml version="1.0" encoding="utf-8"?> 

      <RelativeLayout 
      android:id="@+id/rel" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@drawable/backgroundnr" 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      > 

      <ImageButton 
      android:id="@+id/imageButtons" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/tempText2" 
      android:layout_above="@+id/tempText" 
      android:gravity="center" 
      android:src="@drawable/start" 
      android:background="@null" 
      android:layout_gravity="center_horizontal" 
      > 
      </ImageButton> 

      <TextView 
      android:id="@+id/tempText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Touch to Start" 
      android:layout_centerVertical="true" 
      android:layout_gravity="center_vertical" 
      > 
      </TextView> 

      <ImageView 
      android:id="@+id/bottomlayout" 
      android:layout_width="fill_parent" 
      android:layout_height="100px" 
      android:src="@drawable/bottombar" 
      android:layout_alignParentBottom="true" 
      > 
      </ImageView> 

      <Button 
      android:id="@+id/profileButtons" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Profile" 
      android:drawableTop="@drawable/profiledefault" 
      android:gravity = "bottom" 
      android:background="#0000" 
      android:layout_alignParentBottom="true" 
      android:layout_alignLeft="@+id/bottomlayout" 
      android:textColor="#FFFFFF" 
      android:paddingLeft="10dip" 
      android:paddingBottom="5dip" 
      > 
      </Button> 
      <Button 
      android:id="@+id/savedButtons" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Saved" 
      android:drawableTop="@drawable/favoritesdefault" 
      android:gravity = "bottom" 
      android:background="#0000" 
      android:layout_alignParentBottom="true" 
      android:textColor="#FFFFFF" 
      android:paddingBottom="5dip" 
      android:layout_toRightOf="@+id/profileButtons" 
      android:layout_toLeftOf="@+id/recentButtons" 
      android:layout_gravity="center_horizontal" 
      > 
      </Button> 
      <Button 
      android:id="@+id/recentButtons" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Recent" 
      android:drawableTop="@drawable/recentdefault" 
      android:gravity = "bottom" 
      android:background="#0000" 
      android:layout_alignParentBottom="true" 
      android:textColor="#FFFFFF" 
      android:layout_alignParentRight="true" 
      android:paddingRight="10dip" 
      android:paddingBottom="5dip" 
      > 
      </Button> 
      </RelativeLayout> 
+0

看看layout_weight属性 – Blackbelt

使用

android:layout_centerHorizontal="true"

您的ImageButton,而不是layout_gravity。

并重新排列底部按钮。

不使用leftOf或rightOf作为“savedButtons”的中间按钮。只需使用

android:layout_centerHorizontal="true"

它。而对于左键“profileButtons”使用

android:layout_toLeftOf = "@+id/savedButtons" 

由于右钮“recentButtons”使用

android:layout_toRightOf = "@+id/savedButtons" 

在这个布局中键将占据中心的空间需要,剩下的左右区域用于其余按钮。对于3个按钮中的每一个,您将需要alignParentBottom

HTH。

+0

谢谢。完全工作。只是没有需要注释的部分。 – ahsan