How to add overlay view over other view in android?

末鹿安然 提交于 2019-12-04 00:26:04

问题


I want to put button above image view.

How can i do this?

(Please, don't offer to set Background, cause i need ImageView)


回答1:


Set as background!

Just kidding... what you need is put your views inside a RelativeLayout. Something like will work:

<?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">
  <ImageView
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>
    <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
       android:layout_alignParentBottom="true"
       android:text="blah blah"/>
</RelativeLayout>

Notice the use of params like layout_alignParentLeft which are used to position the view where you want.




回答2:


I agree with @Cristian's answer. but also, If you need button's action listener you can add onClick method to your imageView without using button.

And also ImageButton. I hope this answer shows some path to solve your problem.




回答3:


Worked for me with both ImageView and Button inside RelativeLayout:

 <Button android:layout_centerInParent="true">

...



来源:https://stackoverflow.com/questions/3790511/how-to-add-overlay-view-over-other-view-in-android

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