Android Studio 3.1.4工具栏陷在角落

时间:2018-09-23 07:22:43

标签: android android-layout

很难解释-但是我尝试在项目中添加的所有工具栏似乎都在角落。

我做了一个测试布局以清楚地显示它:测试蓝图工具栏

Test blueprint toolbar

单击图像可放大图像。

这是xml:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="1000dp"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</LinearLayout>

我尝试使用不同的布局。也无法拖动工具栏来调整其大小。

这是我的真实布局 Real layout

和代码

<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/auth_request_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AuthRequestActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/activity_auth_request_toolbar"
    android:layout_width="match_parent"
    android:layout_height="5dp"
    android:layout_alignParentTop="true"
    android:background="@color/somecompanycolor"
    android:minHeight="@dimen/toolbar_height"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

<fragment
    android:id="@+id/activity_auth_request_fragment"
    android:name="someexternalsdk"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_alignParentBottom="true"
    android:layout_below="@id/activity_auth_request_toolbar" />

</android.support.constraint.ConstraintLayout>

我设置工具栏的代码:

mToolbar = findViewById(R.id.activity_auth_request_toolbar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setTitle("Request");

有关更多信息,我的目标是28.0.0-rc02,最大sdk版本28,最小15。

有人知道我在做什么错吗?

2 个答案:

答案 0 :(得分:3)

编辑1

这似乎是Android 28.0.0-rc02上的错误

降级27并依靠build.gradle中的27.1.1将解决此问题

原始答案

您正在使用ConstraintLayout,但是您的视图不受限制

这是通常在未定义任何约束的情况下android studio向我们显示的警告

  

此视图不受限制。它只有设计时位置,所以   在运行时将跳到(0,0)

为这样的工具栏定义约束

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Main2Activity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>

答案 1 :(得分:0)

我也遇到过类似的问题,主要是在使用受限布局时。典型错误是

  1. 渲染问题
  2. 此视图不受限制。它只有设计时间位置,因此除非您添加约束,否则它将在运行时跳至(0,0)
  3. 无法加载具有未知错误的AppCompat ActionBar

非常与您在帖子中描述的问题产生共鸣。查看下面的图像,如果遇到类似的问题,请还原。

Missing Constraints

同样也寻找渲染问题。

Render Problems

解决方案:

我会尝试@Ashwini Violet的建议。

此外,我建议您检查styles.xml文件。如果您有类似以下的内容

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

我建议更改为

<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">

这里的关键词是Base

尝试这些方法,如果需要更多帮助,请还原。高

以下是过去对我有帮助的其他参考资料。

参考文献: