2011-11-28 63 views
2

我有一个包含两个图像按钮的列表标题。我想让其中一个图像按钮位于左侧,另一个位于右侧。这是我的xml文件现在的样子,但它不起作用,它只是把左边的两个图像按钮放在一起。我也试过没有额外的LinearLayouts,但没有运气。Android:布局列表标题

<?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:gravity="fill_horizontal" 
    android:background="@drawable/list_header_background"> 
    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" 
     android:gravity="left" 
     android:background="@drawable/list_header_background"> 
     <ImageButton 
      android:id="@+id/refreshBtn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="4dp" 
      android:src="@drawable/ic_refresh" 
      android:background="@drawable/list_header_background" 
      /> 
    </LinearLayout> 
    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" 
     android:gravity="right" 
     android:background="@drawable/list_header_background"> 
     <ImageButton 
      android:id="@+id/battleBtn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="4dp" 
      android:src="@drawable/ic_battle" 
      android:background="@drawable/list_header_background" 
      /> 
    </LinearLayout> 
</LinearLayout> 

回答

5

由于kabuko写道,RelativeLayout宽度等于父宽度。当我们在RelativeLayout里面有一个子视图时,我们可以使用android:layout_alignParentXXXXXX="true"参数来将它(子对象)与父对象相对应。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:background="@drawable/list_header_background"> 
    <ImageButton 
     android:id="@+id/refreshBtn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="4dp" 
     android:src="@drawable/ic_refresh" 
     android:layout_alignParentLeft="true" 
     android:background="@drawable/list_header_background" 
    /> 
    <ImageButton 
     android:id="@+id/battleBtn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="4dp" 
     android:src="@drawable/ic_battle" 
     android:layout_alignParentRight="true" 
     android:background="@drawable/list_header_background" 
    /> 
</RelativeLayout> 
+0

是的,这是行得通的,我在这个问题上褒贬不一。 kabuko先回答,并且“技术上”是正确的,但你给出了一个更好的例子。 – EpicOfChaos

+0

这取决于你。虽然我在同一时间写了答案。如果它不是源代码,我会更快。但我一直在那里,所以这取决于你。无论如何,这并不重要。这只是虚拟的。 – iCantSeeSharp

+0

我决定给你信贷,因为你有代码示例,但是,也许你可以添加到你的答案是什么,它是关于你的例子,使它的工作,基本上结合你的答案与kabuko,只是一个想法。 – EpicOfChaos

2

您可能希望在按钮上使用带有layout_alignParentLeft和layout_alignParentRight的RelativeLayout。

+0

好的,我会试试,我会报告回来。 – EpicOfChaos