【问题标题】:how to set image inside framelayout如何在framelayout中设置图像
【发布时间】:2016-09-27 12:16:18
【问题描述】:

我想在FrameLayout 内设置图像,但与框架布局的背景重叠

【问题讨论】:

  • 更改 Framelayout 中图像的顺序。您在 FrameLayout 中将图像放在最后。首先,您放置一个图像并放置其他项目。

标签: android xml android-studio overlapping android-framelayout


【解决方案1】:

这是示例,https://snag.gy/IrawbT.jpg

最后放入堆栈的项目将被绘制在它下面的项目之上。 这种布局可以很容易地在其他布局之上绘制, 尤其适用于按钮放置等任务。

要在FrameLayout 中排列子元素,请使用 android:layout_gravity 属性以及任何 android:padding android:margin 你需要。

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frame_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <!-- Child1 is drawn first -->
    <!-- Child2 is drawn over Child1 -->
    <!-- Child3 is drawn over Child1 and Child2 -->
    <TextView
        android:id="@+id/child1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:text="Child 3"
        android:textSize="25sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/child2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="40dip"
        android:src="@color/childs"
        android:text="Child 2" />

    <ImageView
        android:id="@+id/child3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="Image"
        android:src="@color/_dark_gray" />
</FrameLayout >

【讨论】:

  • xml 不正确。在这个顺序中,孩子 3 在堆栈中排在第一位(在所有人之下),孩子 2 在堆栈中排在第二位(在孩子 3 之上),最后孩子 1 在堆栈中排在最后(在所有其他项目之上)
  • 好的,先生。名称可能会改变。一旦他得到他想要的布局,id 在这里真的很重要。谢谢你的建议。
  • 我只提到它是因为 xml 描述如何放置孩子,这与代码不匹配(你给他们的 id)。只是为了更好地理解,仅此而已
  • 我已经更新了我的答案。
【解决方案2】:

这是实现你想要的一种方式

<FrameLayout>

    <ImageView>
    <- Your background image with width and height to match parent ->

    <ImageView>
    <-this is where you put your image->

</FrameLayout>

【讨论】:

  • 这是不对的,因为最后一个孩子总是放在最上面,但是您提到“您的背景图片的宽度和高度与父级匹配”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-15
  • 2016-01-23
  • 1970-01-01
相关资源
最近更新 更多