在android中可以将一个视图放在另一个视图上吗?

kik*_*iki 26 android views overlapping android-videoview

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

但是在线性/相对布局中,视图可以一个接一个地放置,也可以相对于彼此放置,建议不要使用AbsoluteLayout.那我该怎么办?

Ste*_*sen 32

FrameLayout是最简单的,ViewGroupViews按照它们在布局XML中定义的顺序(或以编程方式添加)进行堆叠; 第一个会更低,最后一个会在最顶层.

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

在此输入图像描述

这是具有两个重叠TextView框的实际布局XML .使用android:layout_gravitywhile 完成两个框的偏移,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>
Run Code Online (Sandbox Code Playgroud)

  • 正是我所需要的。 (3认同)
  • 这个答案是纯金的! (2认同)

sko*_*lis 20

FrameLayouts让你将每个视图堆叠在下面的视图之上.这也可以用a来实现RelativeLayout.

  • 你能告诉我一个案例的例子吗?我无法想出来. (4认同)
  • http://stackoverflow.com/questions/6690530/how-to-show-one-layout-on-top-of-the-other-programmatically-in-my-case (2认同)

小智 13

以防万一你想在ButtonView上放置一个视图然后使用它; android:elevation="7dp"对于需要放在按钮顶部的视图.


Eda*_*izi 5

你也可以通过使用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>
Run Code Online (Sandbox Code Playgroud)