是否有可能在android上的工具栏下方设置0,0坐标或使工具栏不可重叠?
我有以下问题: 在我的android程序中,我可以通过点击按钮创建图像视图和按钮。这些新按钮和图像视图将在0,0坐标的左上角创建。问题是我现在包括了一个工具栏,而不是标准的ActionBar。现在创建“我的按钮”和“图像视图”时,它们被工具栏重叠。我需要一种可能性,要么改变工具栏下的坐标系,要么干脆使工具栏不可重叠。 我已经尝试过使用不同的布局,比如在线性布局中包含工具栏,在它下面的主布局等等,但是我发现没有任何效果 我请求帮助,非常感谢 MainActivity.xml是否有可能在android上的工具栏下方设置0,0坐标或使工具栏不可重叠?,android,android-studio,android-layout,Android,Android Studio,Android Layout,我有以下问题: 在我的android程序中,我可以通过点击按钮创建图像视图和按钮。这些新按钮和图像视图将在0,0坐标的左上角创建。问题是我现在包括了一个工具栏,而不是标准的ActionBar。现在创建“我的按钮”和“图像视图”时,它们被工具栏重叠。我需要一种可能性,要么改变工具栏下的坐标系,要么干脆使工具栏不可重叠。 我已经尝试过使用不同的布局,比如在线性布局中包含工具栏,在它下面的主布局等等,但是我发现没有任何效果 我请求帮助,非常感谢 MainActivity.xml <?xml ve
<?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/root_layout_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"/>
<Button
android:id="@+id/aaa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:onClick="bbb"
android:text="@string/ccc"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/ddd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="eee"
android:text="@string/fff"
app:layout_constraintBottom_toTopOf="@+id/ggg"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/hhh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="iii"
android:text="@string/jjj"
app:layout_constraintBottom_toTopOf="@+id/kkk"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/lll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="mmm"
android:text="@string/ooo"
app:layout_constraintBottom_toTopOf="@+id/nnn"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/ppp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="validate"
android:text="@string/qqq"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/rrr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sss"
android:text="@string/ttt"
app:layout_constraintBottom_toTopOf="@+id/uuu"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/vvv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="www"
android:text="@string/button_share"
app:layout_constraintBottom_toTopOf="@+id/xxx"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.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/root_layout_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"/>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize">
<Button
android:id="@+id/aaa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:onClick="bbb"
android:text="@string/ccc"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/ddd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="eee"
android:text="@string/fff"
app:layout_constraintBottom_toTopOf="@+id/ggg"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/hhh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="iii"
android:text="@string/jjj"
app:layout_constraintBottom_toTopOf="@+id/kkk"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/lll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="mmm"
android:text="@string/ooo"
app:layout_constraintBottom_toTopOf="@+id/nnn"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/ppp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="validate"
android:text="@string/qqq"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/rrr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sss"
android:text="@string/ttt"
app:layout_constraintBottom_toTopOf="@+id/uuu"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/vvv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="www"
android:text="@string/button_share"
app:layout_constraintBottom_toTopOf="@+id/xxx"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
编辑:
带有?android属性的MainActivity.xml可以直观地处理问题,但根本无法解决问题
MainActivity.xml
<?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/root_layout_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"/>
<Button
android:id="@+id/aaa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:onClick="bbb"
android:text="@string/ccc"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/ddd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="eee"
android:text="@string/fff"
app:layout_constraintBottom_toTopOf="@+id/ggg"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/hhh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="iii"
android:text="@string/jjj"
app:layout_constraintBottom_toTopOf="@+id/kkk"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/lll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="mmm"
android:text="@string/ooo"
app:layout_constraintBottom_toTopOf="@+id/nnn"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/ppp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="validate"
android:text="@string/qqq"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/rrr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sss"
android:text="@string/ttt"
app:layout_constraintBottom_toTopOf="@+id/uuu"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/vvv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="www"
android:text="@string/button_share"
app:layout_constraintBottom_toTopOf="@+id/xxx"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.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/root_layout_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"/>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize">
<Button
android:id="@+id/aaa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:onClick="bbb"
android:text="@string/ccc"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/ddd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="eee"
android:text="@string/fff"
app:layout_constraintBottom_toTopOf="@+id/ggg"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/hhh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="iii"
android:text="@string/jjj"
app:layout_constraintBottom_toTopOf="@+id/kkk"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/lll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="mmm"
android:text="@string/ooo"
app:layout_constraintBottom_toTopOf="@+id/nnn"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/ppp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="validate"
android:text="@string/qqq"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/rrr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sss"
android:text="@string/ttt"
app:layout_constraintBottom_toTopOf="@+id/uuu"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/vvv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="www"
android:text="@string/button_share"
app:layout_constraintBottom_toTopOf="@+id/xxx"
app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
将工具栏的
.xml
更改为:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="your path to the activity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/your_xml_for_activity_the_one_with_buttons" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
在MainActivity.java中使用:
setContentView(R.layout.toolbar)代码>
像这样,您将创建工具栏,并将使用按钮xml
的内容显示在它下面。以下是一个示例,可以帮助您进行操作,因为我不知道您试图完成的布局是什么,但此布局从工具栏下方开始堆叠按钮
<?xml version="1.0" encoding="utf-8"?>
<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/root_layout_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/aaa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="Button1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/ddd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
app:layout_constraintTop_toBottomOf="@+id/aaa"
app:layout_constraintStart_toStartOf="parent"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/hhh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3"
app:layout_constraintTop_toBottomOf="@+id/ddd"
app:layout_constraintStart_toStartOf="parent" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/lll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4"
app:layout_constraintBottom_toTopOf="@+id/hhh"
app:layout_constraintStart_toStartOf="parent"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/ppp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button5"
app:layout_constraintTop_toBottomOf="@+id/lll"
app:layout_constraintStart_toStartOf="parent"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/rrr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button6"
app:layout_constraintTop_toBottomOf="@+id/ppp"
app:layout_constraintStart_toStartOf="parent"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/vvv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button7"
app:layout_constraintTop_toBottomOf="@+id/rrr"
app:layout_constraintStart_toStartOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
你能把.xml
贴在你创建工具栏的地方吗?我编辑了我的帖子:)将你的约束设置在工具栏下面,目前它们被约束在整个屏幕上的父对象上,所以工具栏将显示所有我实际上已经尝试过的东西的顶部,但我认为问题是,即使我将marginTop设置在工具栏底部,我仍然需要match parent属性。我用android:attr/actionBarSize解决了这个问题,但问题是一样的。也许匹配父项是个问题,但我不知道如何克服。约束到父项顶部是个问题,因为工具栏也在那个位置actionBarSize
将直观地“解决”问题,但在技术上它仍然在工具栏下。我使用此xml(当然需要更改)时遇到以下错误:AAPT:error:resource style/AppTheme.AppBarOverlay(又名com.example.projectname:style/AppTheme.AppBarOverlay)找不到。我还删除了mainactivity.xmlAh中的工具栏。对了,我给了您一个自己项目的示例。添加style.xml
以下内容:
这解决了问题!但现在启动应用程序时似乎出现了一个新问题。。。java.lang.RuntimeException:无法启动activity ComponentInfo{com.example.projectname/com.example.projectname.MainActivity}:android.view.InflateException:二进制XML文件行#2:二进制XML文件行#2:在build:gradle(模块:app)中对类androidx.coordinatorlayout.widget.CoordinatorLayoutAdd进行膨胀时出错
下面的依赖关系
:实现'androidx.coordinatorlayout:coordinatorlayout:1.0.0'
和sync。下一个问题,我记得我曾经遇到过这样一个问题,一直没有解决:Manifest merge failed:Attributeapplication@appComponentFactory值=(androidx.core.app.CoreComponentFactory)来自[androidx.core:core:1.0.0]AndroidManifest.xml:22:18-86也出现在[com.android.support:support compat:28.0.0]AndroidManifest.xml:22:18-91 value=(android.support.v4.app.CoreComponentFactory)。建议:添加“工具:替换=“android:appComponentFactory”'要覆盖AndroidManifest.xml:5:5-19:19中的元素。我想我现在从上面的评论中得到了困惑。问题不是我已经在那里的按钮。这些按钮被限制在我的右下角。我只将它们用于操作。问题是我用这些函数创建的按钮和图像视图。例如,李某ke“create imageView”。此imageView将显示在坐标0,0处,并与我的工具栏重叠。因此app:layout\u constraintStart\u toStartOf=“parent”
和app:layout\u constraintTop\u toBottomOf=“@+id/toolbar”
这里不需要我的按钮,因为它们已经存在。一种解决方案是在创建时在代码中动态设置每个imageView和按钮的约束,但当然最好是简单地约束视图来实现这一点。