我正在尝试将ConstraintLayout功能扩展为具有带有全屏半透明背景色的ProgressBar。我的想法是,我可以重用此布局,并调用hideLoading()
和showLoading()
。进度栏必须居中。
问题是:调用constraintSet.applyTo(this)
时,所有带有ConstraintSet.connect(...)
的 View 都将消失或未绘制。由于对它们的所有操作都将被忽略,并且它们根本不会显示在屏幕上。
这是我所拥有的:
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);
}
}
这是我所拥有的
还有我想要的
最佳答案
问题似乎是您将背景约束设置为MATCH_PARENT
,在ConstraintLayouts中无效。我假设您的意思是LayoutParams.MATCH_CONSTRAINT
。
我假设您希望您的布局“模拟”此XML:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/bkg"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/holo_red_dark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
看起来像:
tl;dr
:将行int matchParent = LayoutParams.MATCH_PARENT;
更改为int matchParent = LayoutParams.MATCH_CONSTRAINT;
更新:我尝试了自己的解决方案,但是没有用,所以我花了几分钟查看您的代码并发现了问题。我正在使用ConstraintLayout 1.1.0
主要 Activity 只是通过
setContentView(R.layout.demo_layout)
夸大其词<?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/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<com.so.ProgressConstraintLayout
android:id="@+id/demo_progress_container"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</android.support.constraint.ConstraintLayout>
还有我的
ProgressConstraintLayout
…是我从您的ojit_code复制并修改的:package com.so;
import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.ConstraintSet;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ProgressBar;
public class ProgressConstraintLayout extends ConstraintLayout {
private View background;
private ProgressBar progressBar;
public ProgressConstraintLayout(Context context) {
super(context);
addLoader();
}
public ProgressConstraintLayout(Context context, AttributeSet attrs) {
super(context, attrs);
addLoader();
}
public ProgressConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
addLoader();
}
private void addLoader() {
int wrapContent = LayoutParams.WRAP_CONTENT;
int matchConstraint = LayoutParams.MATCH_CONSTRAINT;
ConstraintSet constraintSet = new ConstraintSet();
background = new View(getContext());
background.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_bright, getContext().getTheme()));
background.setId(generateViewId());
addView(background, new ConstraintLayout.LayoutParams(matchConstraint, matchConstraint));
progressBar = new ProgressBar(getContext(), null, android.R.attr.progressBarStyleLarge);
progressBar.setIndeterminate(true);
progressBar.setId(generateViewId());
addView(progressBar, new ConstraintLayout.LayoutParams(wrapContent, wrapContent));
constraintSet.clone(this);
constraintSet.connect(background.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
constraintSet.connect(background.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);
constraintSet.connect(background.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START);
constraintSet.connect(background.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END);
constraintSet.connect(progressBar.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
constraintSet.connect(progressBar.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);
constraintSet.connect(progressBar.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START);
constraintSet.connect(progressBar.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END);
constraintSet.applyTo(this);
}
public void showLoading() {
background.setVisibility(VISIBLE);
progressBar.setVisibility(VISIBLE);
}
public void hideLoading() {
background.setVisibility(GONE);
progressBar.setVisibility(GONE);
}
}
如果您想知道它的外观……很无聊,但我是谁呢?