Android studio 3.1.4 工具栏卡在角落里

标签 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。

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

最佳答案

编辑 1

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

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

原始答案

您正在使用ConstraintLayout,但您的 View 不受约束

这是当我们没有定义任何约束时 android studio 向我们显示的警告

This view is not constrained. It only has designtime positions, so it will jump to (0,0) at runtime

像这样定义工具栏约束

<?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>

关于Android studio 3.1.4 工具栏卡在角落里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52463950/

相关文章:

android - 在 ViewStub 中使用 ListView 时出现 NullPointerException

android - 满足条件时从屏幕底部显示 ListView(所有项目可见)

android - 如何将 SearchView 放入高度设置为 wrap_content 的 ToolBar 中而不消失?

java - 使用通用固定标题创建 JPEG 拇指图像

android - 当我尝试上传到 google play 商店时,Cordova 构建和签名错误,没有 v2 签名

android - Windows 上的跨平台开发 (iOS/Android/Blackberry)

android - Native Android -> 如何创建自定义弯曲底部导航

java - ImageButtons 不从主题样式获取属性

android - ListView 忽略 wrap_content

java - 计算两个日期之间的差异: not working as expected