0

我想要,第一个 imagebutton 填充顶部位置,第二个 imagebutton 填充底部位置。我是不是忘记了什么?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="@color/white"
    android:src="@android:drawable/btn_star"
    android:layout_gravity="top" />

<ImageButton
    android:id="@+id/imageButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:background="@color/white"
    android:src="@android:drawable/btn_star"
    android:layout_gravity="bottom" />

</LinearLayout>
4

3 回答 3

1

问题是您需要使用RelativeLayoutTry:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:background="#ffffff"
    android:src="@android:drawable/btn_star"
    android:layout_gravity="top" />

<ImageButton
    android:id="@+id/imageButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:background="#ffffff"
    android:src="@android:drawable/btn_star"
    android:layout_gravity="bottom" />

</RelativeLayout>

在此处输入图像描述

希望有帮助。告诉我这是否是您想要完成的任务,或者您是否希望两个按钮均等地填充一半的屏幕高度。

如果是第二个选项,那么试试这个:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="#ffffff"
    android:src="@drawable/icon"
 />

<ImageButton
    android:id="@+id/imageButton2"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="#ffffff"
    android:src="@drawable/icon"
 />

</LinearLayout>

看起来像这样:

在此处输入图像描述

于 2012-09-01T20:39:06.770 回答
0

如果“填充”是指它应该采用全宽,请使用fill_parent而不是wrap_contentfor android:layout_width。否则你应该使用RelativeLayout你的容器而不是LinearLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageButton
    ...
    android:layout_gravity="top" />

<ImageButton
    ...
    android:layout_gravity="bottom" />

</RelativeLayout>
于 2012-09-01T20:37:31.540 回答
0

尝试使用相对布局。使用它,您可以更轻松地指定按钮的位置。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@color/white"
android:src="@android:drawable/btn_star"
android:layout_gravity="top" />

<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/imageButton1"
android:background="@color/white"
android:src="@android:drawable/btn_star"
android:layout_gravity="bottom" />
    </RelativeLayout>

您可以使用更多位置,例如 layout_below,并且更容易对齐。

更多职位,http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

于 2012-09-01T20:40:26.617 回答