android - 如何在视图位于另一个视图之上的情况下实现这种布局设计
问题描述
我必须编写 a layout
,其中视图与另一个视图重叠。在设计中,当我点击时,bt1
Linearlayout
我们会在正下方看到bt1
带有小动画。我可以设计动画和布局的所有内容,除了考虑如何LinearLayout
放置bt3
和bt4
。
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background">
<TableLayout
android:id="@+id/lv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/custom_toolbar"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<TableRow>
<Button
android:id="@+id/bt1"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_marginEnd="2.5dp"
android:layout_marginStart="3dp"
android:layout_weight="1"
android:background="@color/darkgrey"
android:fontFamily="@font/brandon_re"
android:text="button1"
android:textColor="@color/defaultWhite"
android:textSize="15dp" />
<Button
android:id="@+id/bt2"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_marginEnd="3dp"
android:layout_marginStart="2.5dp"
android:layout_weight="1"
android:background="@color/darkgrey"
android:fontFamily="@font/brandon_re"
android:text="button2"
android:textColor="@color/defaultWhite"
android:textSize="15dp" />
</TableRow>
</TableLayout>
<LinearLayout
android:id="@+id/lv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/lv1"
android:layout_alignBottom="@+id/lv1">
<TextView
android:layout_width="match_parent"
android:layout_height="130dp"
/>
</LinearLayout>
<Button
android:id="@+id/bt3"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginEnd="3dp"
android:layout_marginStart="2.5dp"
android:layout_weight="1"
android:background="@color/darkgrey"
android:fontFamily="@font/brandon_re"
android:layout_below="@+id/lv1"
android:layout_marginTop="20dp"
android:text="button3"
android:textColor="@color/defaultWhite"
android:textSize="15dp" />
<Button
android:id="@+id/bt4"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginEnd="3dp"
android:layout_marginStart="2.5dp"
android:layout_weight="1"
android:background="@color/darkgrey"
android:fontFamily="@font/brandon_re"
android:layout_below="@+id/bt3"
android:layout_marginTop="20dp"
android:text="button4"
android:textColor="@color/defaultWhite"
android:textSize="15dp" />
</RelativeLayout>
解决方案
使用相对布局的 layout_below 属性。
将您的线性布局代码更改为在按钮代码之后并在一个布局中添加按钮。
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
>
...
/* buttons */
</RelativeLayout>
<LinearLayout
android:id="@+id/lv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/lv1">
<TextView
android:layout_width="match_parent"
android:layout_height="130dp"
/>
</LinearLayout>
完整代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:id="@+id/lv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<TableRow>
<Button
android:id="@+id/bt1"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_marginEnd="2.5dp"
android:layout_marginStart="3dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="2.5dp"
android:layout_weight="1"
android:text="button1"
android:textSize="15dp" />
<Button
android:id="@+id/bt2"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_marginEnd="3dp"
android:layout_marginStart="2.5dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="2.5dp"
android:layout_weight="1"
android:text="button2"
android:textSize="15dp" />
</TableRow>
</TableLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/bt3"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginEnd="3dp"
android:layout_marginStart="2.5dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="2.5dp"
android:layout_weight="1"
android:layout_marginTop="20dp"
android:text="button3"
android:textSize="15dp" />
<Button
android:id="@+id/bt4"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_marginEnd="3dp"
android:layout_marginStart="2.5dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="2.5dp"
android:layout_weight="1"
android:layout_below="@+id/bt3"
android:layout_marginTop="20dp"
android:text="button4"
android:textSize="15dp" />
</RelativeLayout>
<LinearLayout
android:id="@+id/lv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
>
<TextView
android:layout_width="match_parent"
android:layout_height="130dp"
/>
</LinearLayout>
</RelativeLayout>
您必须使用重叠布局的背景才能看到重叠..
推荐阅读
- azure - 如何为 Azure SQL 数据库设置 UTF-8 排序规则?UTF-8 不在支持的排序规则列表中
- localization - 为什么人们使用 i18n() 和 i18nc() 而不是 qsTr()?
- ubuntu-16.04 - Ubuntu 16.0 到 18.0 升级后的超级缩放
- javascript - 无法在 tensorflow.js 中加载 python 训练的模型
- wordpress - 无法在 acf/init 中的函数内获取自定义帖子类型 ID
- model-view-controller - TCPConnection IO 异常 Web 应用程序
- visual-studio - 将 asp.net 核心从 2.1.0 升级到 2.1.5
- excel-formula - 去除尾部斜杠的值
- java - 从 MySQL 中检索指纹数据并在 Java 中进行比较?
- php - 使用 Laravel 在特定日期发送提醒邮件