如果使用Match_Constraints,则不会显示嵌套约束布局 [英] Nested Constraint Layout not showing if using Match_Constraints

查看:96
本文介绍了如果使用Match_Constraints,则不会显示嵌套约束布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Android中创建嵌套的ConstraintLayout.

I'm trying to create a nested ConstraintLayout in Android.

目标是在约束布局内的左侧有一个Image,在右边拥有另一个约束布局,如下图所示:

The objective is to have an Image to the left, and another constraint layout to the right, inside a constraint layout, as the following image:

它可以正确显示在预览中,但是在应用程序内部,它会出错并且根本不显示

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中

The layout is being added to another ConstraintLayout using the code

parent.addChild(inflater.inflate(R.layout.box_npc);

我该如何解决?甚至是什么问题?

How should I fix this? And even, what is the issue?

推荐答案

面临的情况是OP试图以编程方式将创建的ConstraintLayout添加到另一个ConstraintLayout.那是用

The situation faced was that OP was trying to add the created ConstraintLayout to another ConstraintLayout programatically. That was being made using

inflater.inflate(R.layout.box, null)

这是不正确的方法,因为这会忽略框中的布局参数.修复问题的原因是

It was an incorrect approach, as this ignores the layout parameters from the box. What was made to fix it was

inflater.inflate(R.layout.box, PARENT_LAYOUT/* One that box was being added to*/)

这解决了一些问题,原因是父布局现在已经尊重布局参数.

This fixes some issues due to the fact that the layout parameters were now being respected by the parent layout.

更多信息:了解Android的Layout Inflater.inflate()

这篇关于如果使用Match_Constraints,则不会显示嵌套约束布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆