【问题标题】:overlapping children in RelativeLayout with percentage以百分比重叠 RelativeLayout 中的子级
【发布时间】:2014-03-20 01:33:44
【问题描述】:

我想将 ImageView 放在另一个视图的顶部(上方),该视图使用宽度和高度的百分比从 SurfaceView 派生。但不知道如何实现这一目标。有什么帮助吗?

到目前为止,这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/main"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
>

    <MyOwnView
        android:id="@+id/my_own_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight=".20"
        android:contentDescription="@string/imageview_description"
        android:src="@drawable/background_picture" />

</RelativeLayout>

这就是我想要的:

【问题讨论】:

  • 确实,我做到了。但这是完全不同的场景,因为有两个元素使用了 30% 和 70% 的空间。在我的场景中,一种观点落后于另一种观点。我从该帖子中获取了我的 ImageView 的 XML 布局。但是没有用。

标签: android android-layout layout android-relativelayout


【解决方案1】:

您需要使用框架布局,获取重叠视图。这是一个小例子。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <View
        android:id="@+id/my_own_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="3"
            android:src="@drawable/ic_launcher" />

        <View
            android:id="@+id/my_own_view"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="7" />
    </LinearLayout>

</FrameLayout>

这是您如何执行此操作的示例。而不是嵌入另一个布局的图像(在我的示例中,我选择了一个线性布局,但它可以是一个相对布局,例如)。在这个内部布局中,您可以像往常一样安排视图。请注意,我使用虚拟视图以及 Imageview 来赋予 weight 一些值。这个权重允许您指定百分比。

我希望这会带你进入正确的方向。

【讨论】:

  • 我在您的示例中没有看到任何 FrameLayout。
  • @Matthias 对不起,这很尴尬,这里的格式化程序认为 是一个 HTML 标签!
  • 好的,但是如何为 ImageView 指定宽度/高度的百分比?
  • 这可以与 FrameLayout 和 RelativeLayout 一起使用。要将 ImageView 放到左上角,您需要在第一个(垂直)布局中再添加一个 LinearLayout(水平)。 layout_weight 必须是零点(.3)
【解决方案2】:

您可以简单地围绕线性布局扭曲 ImageView。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/main"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

<MyOwnView
    android:id="@+id/my_own_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent" 
android:layout_height="match_parent">

   <ImageView
       android:layout_width="wrap_content"
       android:layout_height="0dp"
       android:layout_weight=".20"
       android:contentDescription="@string/imageview_description"
       android:src="@drawable/background_picture" />

</LinearLayout>

</RelativeLayout>

【讨论】:

  • 剩下的 80% 需要另一个占位符(带有一些背景颜色的空 LinearView)。否则它不起作用,因为 ImageView 将显示为 100%。
猜你喜欢
  • 1970-01-01
  • 2023-04-04
  • 2021-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
相关资源
最近更新 更多