Put a button over an ImageView

*爱你&永不变心* 提交于 2019-11-27 14:36:18

问题


I'm newbie in Android.

I would like to know if it's possible to put a button or another component over an ImageView. I've tried setting the image as a background image of a LinearLayout, but when I change between landscape and portrait mode, the image porportions are changed.

Thanks a lot.


回答1:


Don't put the image as a background, you don't have any control on how the image is scaled. Instead, create a RelativeLayout, and put an ImageView as one of the child, and you can place anything (buttons etc) as other RelativeLayout children.

<RelativeLayout ...>
    <ImageView (your image) ...>
    <Button (the button you want) ... />
</RelativeLayout>



回答2:


Try this code... it's Help you....

<?xml version="1.0" encoding="utf-8"?>  
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:orientation="vertical"   
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent">  

 <ImageView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/imageviewMain"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:src="Path "
/>
<Button android:id="@+id/but2" 
     android:layout_width="wrap_content" 
         android:layout_height="wrap_content" />
</RelativeLayout>

Try this Code .....

In that give paramater in button to set your Button Position....

android:layout_margin or android:layout_alignParent

And also give Path of Image....




回答3:


There is another way to do it,http://www.curious-creature.org/2009/03/01/android-layout-tricks-3-optimize-part-1/

Thank you.




回答4:


The simpliest way to do it is to use a FrameLayout.




回答5:


This is easy using the ConstraintLayout.

  1. Set the constraints of the Button to the ImageView.
  2. Drag the Button anywhere you want over the ImageView to position it.
  3. XML code will be autogenerated.



来源:https://stackoverflow.com/questions/7306453/put-a-button-over-an-imageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!