【问题标题】:Android: Placing button relative to ImageViewAndroid:相对于 ImageView 放置按钮
【发布时间】:2014-08-02 18:43:15
【问题描述】:

我想让位图的一部分可点击,我的布局如下所示:

________________________________
|             #####             |
|             #####             |
|      ___________________      |
|      |                 |      |
|      |                 |      |
|      |      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/ 但对于我的简单矩形区域来说,这似乎有点矫枉过正。

【问题讨论】:

    标签: android android-layout android-relativelayout


    【解决方案1】:

    试试这个:

        <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>
    

    【讨论】:

    • 感谢您的快速答复!我应该补充一点,图像被缩放以适应剩余的空间,所以图像左右的空间不是我设置的填充。因此,即使使用 align,按钮也会与相对布局元素对齐,而不是与图像的实际大小对齐。
    【解决方案2】:

    你也可以使用

    <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"
            />
    

    【讨论】:

      【解决方案3】:

      好的,我的 google-fu 改进了,多亏了asenovm,我才弄明白了! imageview 有一个 android:adjustViewBounds 属性,默认设置为 false。将其设置为 true 会调整图像视图的边界,以便 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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多