当 ConstraintSet.applyTo 时,视图消失了

Dan*_*nie 5 android android-constraintlayout

我正在尝试将 ConstraintLayout 功能扩展为具有全屏半透明背景色的 ProgressBar。这个想法是我可以重用这个布局并调用hideLoading()and showLoading()。进度条必须居中。

问题是:当constraintSet.applyTo(this)被调用时ConstraintSet.connect(...),它们上的所有视图都消失了,或者没有绘制。因为对它们的所有操作都被忽略,它们根本不显示在屏幕上。

这是我所拥有的:

public class ConstraintLayout extends android.support.constraint.ConstraintLayout {

    private View background;
    private ProgressBar progressBar;

    public ConstraintLayout(Context context) {
        super(context);
        addLoader();
    }

    public ConstraintLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        addLoader();
    }

    public ConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        addLoader();
    }

    private void addLoader(){
        int wrapContent = LayoutParams.WRAP_CONTENT;
        int matchParent = LayoutParams.MATCH_PARENT;

        ConstraintSet constraintSet = new ConstraintSet();
        constraintSet.clone(this);

        background = new View(getContext());
        background.setBackgroundColor(getResources()
                .getColor(R.color.background_loader_color));
        background.setId(generateViewId());
        addView(background, new ConstraintLayout.LayoutParams(matchParent, matchParent));

        constraintSet.connect(background.getId(), ConstraintSet.TOP, 
                ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0);
        constraintSet.connect(background.getId(), ConstraintSet.BOTTOM, 
                ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0);
        constraintSet.connect(background.getId(), ConstraintSet.LEFT, 
                ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 0);
        constraintSet.connect(background.getId(), ConstraintSet.RIGHT, 
                ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0);

        progressBar = new ProgressBar(getContext(), null, android.R.attr.progressBarStyleLarge);
        progressBar.setId(generateViewId());
        addView(progressBar, new ConstraintLayout.LayoutParams(wrapContent, wrapContent));

        constraintSet.connect(progressBar.getId(), ConstraintSet.TOP, 
                ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0);
        constraintSet.connect(progressBar.getId(), ConstraintSet.BOTTOM, 
                ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0);
        constraintSet.connect(progressBar.getId(), ConstraintSet.START, 
                ConstraintSet.PARENT_ID, ConstraintSet.START, 0);
        constraintSet.connect(progressBar.getId(), ConstraintSet.END, 
                ConstraintSet.PARENT_ID, ConstraintSet.END, 0);

        constraintSet.applyTo(this);
    }

    public void showLoading(){
        background.setVisibility(VISIBLE);
        progressBar.setVisibility(VISIBLE);
    }

    public void hideLoading(){
        background.setVisibility(GONE);
        progressBar.setVisibility(GONE);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我所拥有的 图片 和我想要的 我想要的是

Mar*_*ini 4

问题似乎是您\xe2\x80\x99 正在将背景约束设置为MATCH_PARENT在ConstraintLayouts 中无效。我想你的意思是LayoutParams.MATCH_CONSTRAINT。

\n\n

我假设您希望您的布局 \xe2\x80\x9csimulate\xe2\x80\x9d 这个 XML:

\n\n
<?xml version="1.0" encoding="utf-8"?>\n<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"\n    xmlns:app="http://schemas.android.com/apk/res-auto"\n    android:layout_width="match_parent"\n    android:layout_height="match_parent">\n\n    <View\n        android:id="@+id/bkg"\n        android:layout_width="0dp"\n        android:layout_height="0dp"\n        android:background="@android:color/holo_red_dark"\n        app:layout_constraintBottom_toBottomOf="parent"\n        app:layout_constraintEnd_toEndOf="parent"\n        app:layout_constraintStart_toStartOf="parent"\n        app:layout_constraintTop_toTopOf="parent" />\n\n    <ProgressBar\n        android:id="@+id/progress_bar"\n        android:layout_width="wrap_content"\n        android:layout_height="wrap_content"\n        app:layout_constraintBottom_toBottomOf="parent"\n        app:layout_constraintEnd_toEndOf="parent"\n        app:layout_constraintStart_toStartOf="parent"\n        app:layout_constraintTop_toTopOf="parent" />\n</android.support.constraint.ConstraintLayout>\n
Run Code Online (Sandbox Code Playgroud)\n\n

看起来像:

\n\n

在此输入图像描述

\n\n

tl;dr: 将行更改int matchParent = LayoutParams.MATCH_PARENT;为int matchParent = LayoutParams.MATCH_CONSTRAINT;

\n\n

更新:我尝试了自己的解决方案,但 \xe2\x80\x99 不起作用,所以我花了几分钟查看你的代码并发现了问题。我正在使用 ConstraintLayout 1.1.0

\n\n
    \n
  1. 是的,使用匹配约束。
  2. \n
  3. 您正在为不存在\xe2\x80\x99t 的视图设置约束\xe2\x80\xa6,因此我重新创建了您的\xe2\x80\x9c自定义布局\xe2\x80\x9d 和这里\xe2\x80\x99s 我的:
  4. \n
\n\n

主要活动只是通过setContentView(R.layout.demo_layout)

\n\n
<?xml version="1.0" encoding="utf-8"?>\n<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"\n                                             xmlns:app="http://schemas.android.com/apk/res-auto"\n                                             xmlns:tools="http://schemas.android.com/tools"\n                                             android:id="@+id/root"\n                                             android:layout_width="match_parent"\n                                             android:layout_height="match_parent"\n                                             tools:context=".MainActivity"\n    >\n    <com.so.ProgressConstraintLayout\n        android:id="@+id/demo_progress_container"\n        android:layout_width="0dp"\n        android:layout_height="0dp"\n        app:layout_constraintBottom_toBottomOf="parent"\n        app:layout_constraintEnd_toEndOf="parent"\n        app:layout_constraintStart_toStartOf="parent"\n        app:layout_constraintTop_toTopOf="parent"\n        />\n</android.support.constraint.ConstraintLayout>\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的ProgressConstraintLayout\xe2\x80\xa6 是我从你的复制并修改的:

\n\n
package com.so;\n\nimport android.content.Context;\nimport android.support.constraint.ConstraintLayout;\nimport android.support.constraint.ConstraintSet;\nimport android.util.AttributeSet;\nimport android.view.View;\nimport android.widget.ProgressBar;\n\npublic class ProgressConstraintLayout extends ConstraintLayout {\n\n    private View background;\n    private ProgressBar progressBar;\n\n    public ProgressConstraintLayout(Context context) {\n        super(context);\n        addLoader();\n    }\n\n    public ProgressConstraintLayout(Context context, AttributeSet attrs) {\n        super(context, attrs);\n        addLoader();\n    }\n\n    public ProgressConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr) {\n        super(context, attrs, defStyleAttr);\n        addLoader();\n    }\n\n    private void addLoader() {\n        int wrapContent = LayoutParams.WRAP_CONTENT;\n        int matchConstraint = LayoutParams.MATCH_CONSTRAINT;\n\n        ConstraintSet constraintSet = new ConstraintSet();\n\n        background = new View(getContext());\n        background.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_bright, getContext().getTheme()));\n        background.setId(generateViewId());\n        addView(background, new ConstraintLayout.LayoutParams(matchConstraint, matchConstraint));\n\n        progressBar = new ProgressBar(getContext(), null, android.R.attr.progressBarStyleLarge);\n        progressBar.setIndeterminate(true);\n        progressBar.setId(generateViewId());\n        addView(progressBar, new ConstraintLayout.LayoutParams(wrapContent, wrapContent));\n\n        constraintSet.clone(this);\n        constraintSet.connect(background.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);\n        constraintSet.connect(background.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);\n        constraintSet.connect(background.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START);\n        constraintSet.connect(background.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END);\n        constraintSet.connect(progressBar.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);\n        constraintSet.connect(progressBar.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);\n        constraintSet.connect(progressBar.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START);\n        constraintSet.connect(progressBar.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END);\n        constraintSet.applyTo(this);\n    }\n\n    public void showLoading() {\n        background.setVisibility(VISIBLE);\n        progressBar.setVisibility(VISIBLE);\n    }\n\n    public void hideLoading() {\n        background.setVisibility(GONE);\n        progressBar.setVisibility(GONE);\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果你\xe2\x80\x99想知道它看起来如何\xe2\x80\xa6很无聊,但我凭什么来判断:\n在此输入图像描述

\n