我正在尝试在 Android 中创建一个嵌套的 ConstraintLayout。
目标是在约束布局内左侧有一个图像,右侧有另一个约束布局,如下图所示:
It correctly shows on the preview, but inside the application, it bugs and doesn't show at all
布局文件:
<?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:id="@+id/box_npcs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/npcImage"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="@+id/layoutInformation"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="@+id/layoutInformation"
app:srcCompat="@color/colorBlueishGreen" />
<android.support.constraint.ConstraintLayout
android:id="@+id/layoutInformation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:maxWidth="250dp"
app:layout_constraintHorizontal_bias="0.021"
app:layout_constraintLeft_toRightOf="@+id/npcImage"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/top_priority"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ellipsize="start"
android:text="DummyTextIsDummy"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="@color/common_google_signin_btn_text_light_default"
android:textSize="24sp"
android:textStyle="bold"
android:typeface="normal"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
正在使用代码将布局添加到另一个 ConstraintLayout
parent.addChild(inflater.inflate(R.layout.box_npc);
我应该如何解决这个问题?甚至,问题是什么?
面临的情况是OP试图以编程方式将创建的ConstraintLayout添加到另一个ConstraintLayout。那是用做的
inflater.inflate(R.layout.box, null)
这是一个不正确的方法,因为它忽略了框中的布局参数。修复它的方法是
inflater.inflate(R.layout.box, PARENT_LAYOUT/* One that box was being added to*/)
这修复了一些问题,因为布局参数现在受到父布局的尊重。
更多信息:了解 Android Layoutinflater.inflate() https://www.bignerdranch.com/blog/understanding-androids-layoutinflater-inflate/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)