[英]How can I overlay an ImageView with translucent views?
I have an ImageView in an activity that takes up the whole screen. 我在一个占用整个屏幕的活动中有一个ImageView。 What I want to do is have a few translucent buttons in the corner of this ImageView overlayed on top (like 30% transparency). 我想做的是在此ImageView的角上有一些半透明的按钮,该按钮叠加在顶部(例如30%的透明度)。 Is this possible with an ImageView in android? 这可能与Android中的ImageView一起使用吗? If it is can someone point me in the right direction to get started? 如果可以,有人可以指出正确的方向开始学习吗?
Use a layout, and make your ImageView and two Buttons children within the layout. 使用一种布局,并使您的ImageView和两个Buttons子级在布局中。
Example using RelativeLayout: 使用RelativeLayout的示例:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/image"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="0.5"
android:text="Button 1"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/button1"
android:alpha="0.5"
android:text="Button 2"/>
</RelativeLayout>
You can position your buttons better by using android:layout_marginTop and android:layout_marginLeft attributes. 您可以使用android:layout_marginTop和android:layout_marginLeft属性更好地放置按钮。
The key parts to understand here are: 这里要理解的关键部分是:
1/ The ImageView is set to match_parent
, therefore it'll stretch to fill the RelativeLayout. 1 / ImageView设置为match_parent
,因此它将拉伸以填充RelativeLayout。
2/ By default, sub Views are positioned at the top left of RelativeLayouts, this is why button1 appears there. 2 /默认情况下,子视图位于RelativeLayouts的左上方,这就是button1出现在其中的原因。
3/ Button2 is positioned to the right of button1 using the RelativeLayout attribute layout_toRightOf
. 3 /使用RelativeLayout属性layout_toRightOf
将Button2定位在button1的右侧。 Its vertical position is still set to the default - top. 其垂直位置仍设置为默认值-top。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.