这很难解释——但是我尝试在项目中添加的所有工具栏似乎都卡在了角落里。
我做了一个测试布局来清楚地显示它:测试蓝图工具栏
点击图片查看大图。
这是 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>
我尝试过使用不同的布局。也无法拖动工具栏来调整其大小。
和代码
<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/