【问题标题】:How do you place an item behind another item in android?你如何在android中将一个项目放在另一个项目后面?
【发布时间】:2014-08-09 11:14:27
【问题描述】:

我想在ImageButton 后面放置一个ImageView。这样做的最佳方法是什么?

【问题讨论】:

  • 我可以对 -1 发​​表评论吗?

标签: android


【解决方案1】:

我相信最简单的方法是将ImageView 和ImageButton 包装在FrameLayout 中。例如:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/your_image" />
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center" />
</FrameLayout>

【讨论】:

    【解决方案2】:

    您可以在某些布局中重叠视图(例如FrameLayout、RelativeLayout)。

    只需将两个视图放在 xml 布局中,以便重叠(例如,对于 FrameLayout,宽度和高度都为 match_parent)。您添加的最后一个在顶部。

    【讨论】:

      【解决方案3】:

      或者你可以使用RelativeLayout,就像 matiash 说的:

      以下是包含您的个人资料 Facebook 图片的导航抽屉的自定义标题的示例代码

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:facebook="http://schemas.android.com/apk/res-auto"
      android:id="@+id/customHeader"
      android:layout_width="match_parent" android:layout_height="match_parent">
      
       <ImageView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:src="@drawable/green_gradient"/>
      
      <com.facebook.widget.ProfilePictureView
          android:id="@+id/leftPic"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentBottom="true"
          facebook:preset_size="small"
          android:paddingBottom="4dp"
          android:paddingLeft="3dp"
          />
      
      </RelativeLayout>
      

      结果

      你可以在你想要的地方添加一个 ImageButton,只需将以下代码添加到你的布局中

      <ImageButton
      android:id="@+id/myImageButton
      android:layout_width="SET YOUR WIDTH"
      android:layout_height="SET YOUR HEIGHT"
      android:background="ADD YOUR BACKGROUND" />
      

      【讨论】:

        【解决方案4】:

        最简单的方法是只使用ImageButton。

        ImageButton 有一个background 属性作为背景图片

        以及前景中图像的src 属性。

        在 StackOverflow 上查看这个现有的 example。

        <ImageButton
            android:id="@+id/ImageButton01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/album_icon"
            android:background="@drawable/round_button" />
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-02-05
          • 1970-01-01
          • 2016-03-26
          • 2018-09-26
          • 1970-01-01
          • 2014-09-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多