【发布时间】:2018-01-28 15:50:48
【问题描述】:
我的 ConstraintLayout 出现以下问题
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:background="@color/colorPrimary"
android:minHeight="?android:attr/actionBarSize"
android:orientation="horizontal"
android:paddingEnd="8dp">
<android.support.constraint.ConstraintLayout
android:id="@+id/layout_titlemode"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textAlignment="center"
android:textColor="@color/colorUnactive"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5"/>
</android.support.constraint.ConstraintLayout>
</LinearLayout>
样式(使用 AppTheme):
<resources>
<style name="AppTheme" parent="Theme.MultiBackStack">
<item name="android:textColorPrimary">@color/colorTextPrimary</item>
</style>
<style name="Theme.MultiBackStack" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimary</item>
<item name="android:textColorPrimary">@color/colorTextPrimary</item>
</style>
</resources>
这是我对预览的预期:
我除了TextView 会占用所有可用空间(如 match_parent)并且文本显示在视图的中心之外。
这是实际显示的内容:
实际上没有显示任何内容(我猜是因为 layout_width="0dp")
新增约束支持库最新版本: 'com.android.support.constraint:constraint-layout:1.0.2'
我还尝试了什么:
我添加到TextView app:layout_constraintWidth_default="wrap"
这让TextView 出现了,但它从左侧开始并且没有居中。我怎样才能使视图居中?
我无法确定为什么什么都没有显示。
提前感谢您的帮助!
【问题讨论】:
-
检查你的主题@ Hanno
-
@A.N.T 我将主题设置放入帖子中。 ConstraintLayout 需要哪个父级才能正常工作?
-
试试我的主题,因为它适合我@Hanno
-
感谢您的帮助!我在 ActionBar 中使用约束布局。即使使用您提供的主题,它也不起作用。我正在从 ActionBar 更改为 Toolbar 以使其正常工作。
标签: android android-layout centering android-constraintlayout