【问题标题】:Put a button over an ImageView在 ImageView 上放置一个按钮
【发布时间】:2011-09-05 09:58:07
【问题描述】:

我是 Android 新手。

我想知道是否可以在 ImageView 上放置一个按钮或其他组件。我尝试将图像设置为 LinearLayout 的背景图像,但是当我在横向和纵向模式之间切换时,图像部分会改变。

非常感谢。

【问题讨论】:

  • 你能发布你的代码吗?

标签: android imageview


【解决方案1】:

不要将图像作为背景,您无法控制图像的缩放方式。相反,创建一个 RelativeLayout,并将 ImageView 作为其中一个子项,您可以将任何东西(按钮等)作为其他 RelativeLayout 子项。

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

【讨论】:

  • 非常感谢。我希望我能提高我的 android 知识并帮助很多人,
  • 似乎资源中的顺序很重要,将 ImageView 放在 Button 之后对我不起作用,但上面的顺序可以。
【解决方案2】:

试试这个代码......它可以帮助你......

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

试试这个代码.....

在按钮中给参数设置按钮位置....

android:layout_margin or android:layout_alignParent

并且还给出图像路径....

【讨论】:

    【解决方案3】:
    【解决方案4】:

    最简单的方法是使用FrameLayout

    【讨论】:

      【解决方案5】:

      这很容易使用ConstraintLayout

      1. Button的约束设置为ImageView
      2. Button 拖动到ImageView 上方的任意位置以定位它。
      3. 将自动生成 XML 代码。

      【讨论】:

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