2012-09-01 85 views
0

我想要的是,第一個圖像按鈕填充頂部的位置,第二個圖像按鈕填充底部位置。我忘了什麼嗎?android - 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> 

回答

1

的問題是,你需要使用一個RelativeLayout嘗試:

<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> 

enter image description here

希望有所幫助。告訴我,如果這是你想要完成的,或者如果你想要兩個按鈕來平均填充屏幕高度的一半。

如果是,則第二個選項,那麼試試這個:

<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> 

這將是這樣的:

enter image description here

0

如果「補」你的意思是應該採取全寬度,使用fill_parent代替wrap_contentandroid: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> 
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