我正在尝试将ConstraintLayout功能扩展为具有全屏半透明背景颜色的ProgressBar。我的想法是,我可以重复使用此布局并调用hideLoading()
和showLoading()
。进度条必须居中。
问题:调用constraintSet.applyTo(this)
时,所有ConstraintSet.connect(...)
上的视图都是GONE,或者未绘制。由于忽略了它们上的所有操作,它们根本不会显示在屏幕上。
这就是我所拥有的:
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);
}
}
答案 0 :(得分:3)
问题似乎是您将背景约束设置为JSONObject jObj4 = jObj3.getJSONObject("landmark").getJSONObject("contour_chin");
,这在ConstraintLayouts中无效。我认为你的意思是MATCH_PARENT
。
我假设你希望你的布局“模拟”这个XML:
LayoutParams.MATCH_CONSTRAINT
看起来像:
<?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;
更新:我尝试了自己的解决方案并且无法正常工作,因此我花了几分钟时间查看代码并发现了问题。我正在使用ConstraintLayout 1.1.0
主要活动只是通过int matchParent = LayoutParams.MATCH_CONSTRAINT;
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
答案 1 :(得分:1)
自1.1.0-beta5以来发生了一些变化,现在需要代码明确设置视图的宽度和高度,以手动定义ConstraintLayout
及其ConstraintSet
。我不确定原因,但代码如下:
连接background
添加
constraintSet.constrainWidth(background.getId(),matchParent);
constraintSet.constrainHeight(background.getId(),matchParent);
连接progressBar
添加
constraintSet.constrainWidth(progressBar.getId(),wrapContent);
constraintSet.constrainHeight(progressBar.getId(),wrapContent);
如果没有此代码,视图的宽度和高度不会在约束内设置。此代码确保设置宽度和高度。