android - 操作栏下方的 Android 布局
问题描述
我想制作一个带有多个按钮的屏幕,但是,操作栏下的按钮不断出现类似这样的内容。错误的按钮这些按钮稍后以编程方式添加到LinearLayout
.
xml 文件看起来像activity_main.xml:
<androidx.coordinatorlayout.widget.CoordinatorLayout
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:id="@+id/coordinator_layout"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize">
</FrameLayout>
<include
layout="@layout/content_main"
android:layout_width="331dp"
android:layout_height="672dp"
app:layout_anchor="@+id/container"
app:layout_anchorGravity="center"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
content_main.xml:
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainScreen"
tools:context=".MainActivity"
>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="415dp"
android:layout_height="606dp"
android:fitsSystemWindows="true"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
tools:context=".MainActivity" />
<Button
android:id="@+id/button"
android:layout_width="108dp"
android:layout_height="86dp"
android:layout_marginBottom="2dp"
android:background="@android:drawable/ic_input_add"
android:onClick="newScheme"
android:tag="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.997"
tools:ignore="OnClick" />
</androidx.constraintlayout.widget.ConstraintLayout>
如何获取操作栏下方而不是其下方的按钮?
解决方案
我创建了这样的东西,但我改变了一些硬编码的大小(我不知道你做了什么,但我认为通常这不是一个好习惯)。
主要 XML
<androidx.coordinatorlayout.widget.CoordinatorLayout
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:id="@+id/coordinator_layout"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<include
layout="@layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_anchor="@+id/container"
app:layout_anchorGravity="center" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
content_main.XML
<androidx.constraintlayout.widget.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/mainScreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:fitsSystemWindows="true"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 4" />
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="108dp"
android:layout_height="86dp"
android:background="@android:drawable/ic_input_add"
android:onClick="newScheme"
android:tag="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="OnClick" />
</androidx.constraintlayout.widget.ConstraintLayout>
在这里我添加了按钮,但如果您以编程方式添加它,只需将其删除。
还有一个效果:
推荐阅读
- powershell - 在 CircleCI orb 内的作业中的 PowerShell 步骤中使用环境变量
- amazon-web-services - AWS Lambda Cloudwatch 目标到多个 url
- piral - 将 Blazor Pilet 发布到 Feed 服务时出错
- react-native - 更新点 Redux React Native
- c# - 在 winform 应用程序中查找不需要的 TCP 数据包的来源
- javascript - 网站上的 javascript 和用户输入问题以进行暗模式切换
- json - Flutter JSON - 如何获取变量的引号?
- python - 如何在 Abaqus 中通过数组列表更改模型名称?
- numpy - sympy 最大似然:对书中脚本的澄清
- excel - 如何获取刷新数据透视表的最后日期和时间?