我想让位图的一部分可以点击,我的布局如下:
________________________________
| ##### |
| ##### |
| ___________________ |
| | | |
| | | |
| | image | |
| | | |
| | | |
| ___________________ |
________________________________
我认为最简单的方法是在图像上放置一个相对布局的按钮:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/ImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ImageView>
<Button
android:id="@+id/Button"
android:layout_width="200dip"
android:layout_height="150dip"/>
</RelativeLayout>
但我还没有弄清楚如何让按钮与图像的左上角对齐(而不是像现在这样与相对布局的左上角落对齐)。这在相对布局的情况下可能吗?
其他方法的建议也很受欢迎,我认为:http://blahti.wordpress.com/2012/06/26/images-with-clickable-areas/但对于我的简单矩形区域来说,这似乎有点过头了。
试试这个:
<ImageView
android:id="@+id/ImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ImageView>
<Button
android:id="@+id/Button"
android:layout_alignLeft="@id/ImageView"
android:layout_alignTop = "@id/ImageView"
android:layout_width="200dip"
android:layout_height="150dip"/>
</RelativeLayout>
您也可以使用
<Button
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/image1"
android:drawableLeft="@drawable/image2t"
android:drawableRight="@drawable/image3"
android:drawableTop="@drawable/image4"
android:drawableTop="@drawable/image4"
android:textColor="#ffffff"
/>
好吧,我的谷歌功能有所改进,多亏了asenovm,我才明白了!imageview有一个android:adjustViewBounds属性,默认设置为false。如果将其设置为true,则会调整图像的imageviews边界,以便alignLeft和alignTop能正确地为按钮工作。
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/ImageView"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ImageView>
<Button
android:id="@+id/Button"
android:layout_alignLeft="@id/ImageView"
android:layout_alignTop = "@id/ImageView"
android:layout_width="200dip"
android:layout_height="150dip"/>
</RelativeLayout>