如何在样式中使用约束属性?
当我试图像使用其他带有自定义命名空间的属性一样使用它时,它对我的视图没有影响。
<style name="Header.Center" parent="Header">
<item name="layout_constraintBottom_toBottomOf">parent</item>
</style>
添加命名空间app:并没有起到帮助作用。
如何在样式中使用约束属性?
当我试图像使用其他带有自定义命名空间的属性一样使用它时,它对我的视图没有影响。
<style name="Header.Center" parent="Header">
<item name="layout_constraintBottom_toBottomOf">parent</item>
</style>
添加命名空间app:并没有起到帮助作用。
首先,请确保您应用样式的 View
是 ConstraintLayout
的直接子级。否则,约束将在定位 View
时不被考虑。
我已经尝试过了,您尝试的方法确实可行。我已经将以下样式添加到 styles.xml
中:
<style name="CustomStyle">
<item name="layout_constraintBottom_toBottomOf">parent</item>
<item name="layout_constraintEnd_toEndOf">parent</item>
</style>
创建了一个基本的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/CustomStyle"/>
</android.support.constraint.ConstraintLayout>
它确实将TextView
定位于父视图的右下角。
<TextView
android:text="Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/CustomStyle"/>