[英]How to place ImageButton over ImageView in RelativeLayout
我想在我的RelativeLayout中使用兩個ImageButtons和一個ImageView來做這個。 是否可以在不使用marginTop的情況下執行此操作? 歡迎任何回復。 我的xml代碼:
<ImageView
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="fitXY"
android:layout_alignParentBottom = "true"
android:src="@drawable/image1"/>
<ImageButton
android:id="@+id/imagebutton1"
android:layout_width="70dp"
android:layout_height="50dp"
android:scaleType="fitXY"
android:layout_alignParentRight="true"
android:layout_alignParentBottom = "true"
android:background="@drawable/image2"/>
<ImageButton
android:id="@+id/imagebutton2"
android:layout_width="70dp"
android:layout_height="50dp"
android:scaleType="fitXY"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom = "true"
android:background="@drawable/image3"/>
RelativeLayouts幾乎總是正確的布局,它們非常強大。 在你的情況下,你可以簡單地使用'android:layout_centerVertical =“true”',但擺脫'layout_alignParentBottom'
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="150dp">
<ImageView
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="fitXY"
android:layout_alignParentBottom = "true"
android:src="@drawable/image1"/>
<ImageButton
android:id="@+id/imagebutton1"
android:layout_width="70dp"
android:layout_height="50dp"
android:scaleType="fitXY"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/image2"/>
<ImageButton
android:id="@+id/imagebutton2"
android:layout_width="70dp"
android:layout_height="50dp"
android:scaleType="fitXY"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="@drawable/image3"/>
</RelativeLayout>
根據我的個人經驗:如有疑問,請使用RelativeLayout。
我建議在這種情況下使用FrameLayout
。 在FrameLayout
,z-index由添加項的順序定義。 因此, FrameLayout
的最后一個元素應該是ImageButton
以便它將在ImageView
之上繪制。
你應該使用Framelayout,然后先添加imageView,然后再添加imageButton。
請點擊此鏈接: http : //code.tutsplus.com/tutorials/android-user-interface-design-frame-layouts--mobile-4877
http://javatechig.com/android/android-framelayout-example
與Relativelayout你不能。
試試android:layout_centerVertical="true"
。
如果我能正確得到您的答案,那么您可以在代碼中執行此操作。 分配完所有視圖后( findViewById(...)
調用) findViewById(...)
下行添加到您的代碼中:
mMyImageButton.bringToFront();
希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.