在android中是否可以将一个视图放置在另一个视图上?

发布于 2024-09-25 13:14:07 字数 170 浏览 1 评论 0原文

我们可以将一个小视图放在另一个大视图之上吗?例如,我有一个 VideoView 正在后台播放文件。在此之上,在中间/角落的某个地方,我想放置另一个 ImageView。

但在 Linear/Relative Layout 中,视图只能依次放置或相对放置,不建议使用 AbsoluteLayout。那我该怎么办?

Can we place a small view over another large view? For example, I have a VideoView which is playing a file in the background. Over this, somewhere in the middle/corner, I want to place another ImageView.

But in Linear/Relative Layout, views can be placed only one after another or relative to each other, and AbsoluteLayout is advised against. So what do I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

不美如何 2024-10-02 13:14:07

FrameLayout 是最简单的 ViewGroup,它按照布局 XML 中定义(或以编程方式添加)的顺序堆叠 View;第一个将较低,最后一个将在顶部。

以下是两个 View 堆叠并偏移的示例,以更好地说明这一点:

< img src="https://i.sstatic.net/KnUgf.png" alt="在此处输入图像描述">

这是带有两个重叠 TextView 框的实际布局 XML 。两个框的偏移是使用 android:layout_gravity 完成的,而 android:gravity 用于将文本本身在每个框内居中。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="100dp">

    <TextView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="top|left"
        android:background="@android:color/holo_blue_light"
        android:gravity="center"
        android:text="First is below"/>

    <TextView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="bottom|right"
        android:background="@android:color/holo_green_light"
        android:gravity="center"
        android:text=" Last is on top"/>

</FrameLayout>

The FrameLayout is the simplest ViewGroup and stacks the Views in the order they're defined in layout XML (or added programmatically); the first will be lower, and the last will be on top.

Here is an example where two Views are stacked and offset to better illustrate the point:

enter image description here

Here is the actual layout XML with the two overlapping TextView boxes. The offset of the two boxes is done using android:layout_gravity while android:gravity is used for centering the text itself within each box.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="100dp">

    <TextView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="top|left"
        android:background="@android:color/holo_blue_light"
        android:gravity="center"
        android:text="First is below"/>

    <TextView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="bottom|right"
        android:background="@android:color/holo_green_light"
        android:gravity="center"
        android:text=" Last is on top"/>

</FrameLayout>
街角迷惘 2024-10-02 13:14:07

以防万一,如果您想将视图放置在 ButtonView 之上,请使用它;
android:elevation="7dp" 用于需要放置在按钮顶部的视图。

Just in case if you want to place a view on top of a ButtonView then use this;
android:elevation="7dp" for the view which needs to be placed on top of the button.

z祗昰~ 2024-10-02 13:14:07

FrameLayouts 允许您将每个视图堆叠在下面的视图之上。这也可以通过 RelativeLayout 来实现。

FrameLayouts let you pile each view on top of the one below. This can also be achieved with a RelativeLayout.

仅此而已 2024-10-02 13:14:07

您还可以使用 Google 引入的新布局 ConstraintLayout 来实现此目的。

ConstraintLayout 允许您使用平面视图层次结构(无嵌套视图组)创建大型且复杂的布局。

 <?xml version="1.0" encoding="utf-8"?>
  <android.support.constraint.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:layout_weight="4"
  tools:context="com.edalat.example.MainActivity">
  <VideoView
    android:id="@+id/videoView"
    android:layout_width="283dp"
    android:layout_height="349dp"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="24dp"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="24dp"
    android:layout_marginRight="24dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginLeft="24dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintHorizontal_bias="0.509"/>
  <ImageView
     android:id="@+id/imageView"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     app:srcCompat="@mipmap/ic_launcher"
     app:layout_constraintTop_toTopOf="parent"
     android:layout_marginTop="24dp"
     app:layout_constraintBottom_toBottomOf="parent"
     android:layout_marginBottom="24dp"
     android:layout_marginLeft="24dp"
     app:layout_constraintLeft_toLeftOf="parent"
     android:layout_marginRight="24dp"
     app:layout_constraintRight_toRightOf="parent"/>
   </android.support.constraint.ConstraintLayout>

You can also do it by using ConstraintLayout a new layout introduced by google.

ConstraintLayout allows you to create large and complex layouts with a flat view hierarchy (no nested view groups).

 <?xml version="1.0" encoding="utf-8"?>
  <android.support.constraint.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:layout_weight="4"
  tools:context="com.edalat.example.MainActivity">
  <VideoView
    android:id="@+id/videoView"
    android:layout_width="283dp"
    android:layout_height="349dp"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="24dp"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="24dp"
    android:layout_marginRight="24dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginLeft="24dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintHorizontal_bias="0.509"/>
  <ImageView
     android:id="@+id/imageView"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     app:srcCompat="@mipmap/ic_launcher"
     app:layout_constraintTop_toTopOf="parent"
     android:layout_marginTop="24dp"
     app:layout_constraintBottom_toBottomOf="parent"
     android:layout_marginBottom="24dp"
     android:layout_marginLeft="24dp"
     app:layout_constraintLeft_toLeftOf="parent"
     android:layout_marginRight="24dp"
     app:layout_constraintRight_toRightOf="parent"/>
   </android.support.constraint.ConstraintLayout>
z祗昰~ 2024-10-02 13:14:07
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/dp_20"
    android:background="@color/yellow"
    >
        <VideoView
            android:id="@+id/videoview"
            android:layout_width="wrap_content"
            android:layout_centerInParent="true"
            android:layout_height="300dp"
            android:contentDescription="@string/app_name"
             />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/camera"
            android:layout_margin="30dp"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:id="@+id/imageView" />

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/dp_20"
    android:background="@color/yellow"
    >
        <VideoView
            android:id="@+id/videoview"
            android:layout_width="wrap_content"
            android:layout_centerInParent="true"
            android:layout_height="300dp"
            android:contentDescription="@string/app_name"
             />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/camera"
            android:layout_margin="30dp"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:id="@+id/imageView" />

</RelativeLayout>
从﹋此江山别 2024-10-02 13:14:07

创建相对布局并将视图放入其中,并添加

android:elevation="2dp" 

要位于另一个视图之上的视图

Create Relative Layout and place views inside it and add

android:elevation="2dp" 

for the view whcich is to be over another one

二货你真萌 2024-10-02 13:14:07

通过将 XML 文件中的后向布局放在前向布局之前,可以轻松完成此操作。

例如,如果我想 cardView1 出现在 cardView2 上,我会编写以下代码:

<androidx.cardview.widget.CardView
        android:id="@+id/cardView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

<androidx.cardview.widget.CardView
        android:id="@+id/cardView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

由于 cardView2 出现在 cardView1 之前,< code>cardView1 是位于另一个之上的一个。

It can be done easily by putting the back layouts before forwarded layouts in XML file.

for example if I want to cardView1 comes over cardView2 I write this code:

<androidx.cardview.widget.CardView
        android:id="@+id/cardView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

<androidx.cardview.widget.CardView
        android:id="@+id/cardView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

Since cardView2 comes before cardView1 , cardView1 is the one that comes over the other one.

梦中的蝴蝶 2024-10-02 13:14:07

解决了完全相同的问题。我通过将所需的元素放置在两个布局之间并使用 android:layout_marginTop 属性的负值来做到这一点。

这是代码:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="-35dp">

以及它在布局中的外观:

我还不允许发布图片,所以这里是一个链接

Quite the same problem solved. I did it by placing the needed element between two layouts and using negative value for android:layout_marginTop attribute.

Here's the code:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="-35dp">

And how it looks in the layout:

I'm not allowed to post pictures yet, so here's a link to it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文