Android 将所有四个约束移动到themes.XML时不应用约束
我有一个ConstraintLayout,其中我在中间放置了一个ProgressBarAndroid 将所有四个约束移动到themes.XML时不应用约束,android,android-layout,android-constraintlayout,android-theme,Android,Android Layout,Android Constraintlayout,Android Theme,我有一个ConstraintLayout,其中我在中间放置了一个ProgressBar <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progress_bar"
style="@style/ProgressBarStyle"/>
</androidx.constraintlayout.widget.ConstraintLayout>
我得到以下警告:
此视图不受约束。它只有设计时间位置,因此在运行时它将跳转到(0,0),除非添加约束
因此,即使themes.XML文件中存在约束,我仍然会收到此警告。如何解决此问题?因为无法在样式中为视图设置约束。约束应在约束布局下。编写xml代码时,布局尝试查找所有约束以将视图放置在预览窗口中。它们不会查看约束的样式。这就是为什么您会收到警告。要避免警告,请将所有四个约束从样式移动到布局。我过去遇到过这种情况-只是看起来在检查约束时没有处理。你没有做错什么
tools:ignore=“missingstraints”
将消除该错误。您可以将其应用于显示错误的每个视图或ConstraintLayout XML中,以便将其应用于整个布局。模拟器上发生了什么?@Marat也位于模拟器的中心,而不仅仅是Android Studio中。但为什么会出现这种错误呢?约束已经存在。感谢您的回复。你有这方面的官方文件吗?为什么其他属性可以正常工作,而只有约束不能正常工作?为什么Android Studio和emulator看起来都很好?不,我在文档中没有看到任何东西。但是当我尝试用样式编写可重用代码时,我得到了相同的pbm。正如我所说,它只显示警告,在运行时样式会起作用。@joan PSo我能保持这种方式,只抑制警告吗?或者我会有麻烦吗?我想最好转到布局。好的,谢谢。刚刚投票决定了你的答案。我希望我也能得到其他答案。
<style name="ProgressBarStyle">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="layout_constraintBottom_toBottomOf">parent</item>
<item name="layout_constraintEnd_toEndOf">parent</item>
<item name="layout_constraintStart_toStartOf">parent</item>
<item name="layout_constraintTop_toTopOf">parent</item>
</style>