以编程方式更改约束布局中的准则百分比 [英] Change Guideline percentage in constraint layout programmatically
问题描述
我在这样的约束布局中有一条准则
I have a guideline in constraint layout like this
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline8"
app:layout_constraintGuide_percent="0.5"
android:orientation="vertical"/>
稍后我想有条件地将app:layout_constraintGuide_percent
值更改为其他值.我该如何做到这一点.
Later I want to change the app:layout_constraintGuide_percent
value to something else conditionally. How can I achieve this.
推荐答案
有两种方法可以做到:
此方法从
ConstraintLayout
获取特定参数并对其进行修改.
This method get a specific parameter from the
ConstraintLayout
and modify it.
Guideline guideLine = (Guideline) findViewById(R.id.your_guideline);
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) guideLine.getLayoutParams();
params.guidePercent = 0.45f; // 45% // range: 0 <-> 1
guideLine.setLayoutParams(params);
- 使用
ConstraintSet
此方法包括克隆
ConstraintLayout
属性,对其进行修改,然后将其应用于View
.
This method consists in cloning the
ConstraintLayout
attributes, modifying them and then applying to theView
.
但是,它非常慢.
ConstraintLayout constraintLayout = (ConstraintLayout) findViewById(R.id.your_constraint_with_guideline);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
constraintSet.setGuidelinePercent(R.id.your_guideline, 0.07f); // 7% // range: 0 <-> 1
TransitionManager.beginDelayedTransition(constraintLayout);
constraintSet.applyTo(constraintLayout);
创建视差效果
为了测试这些方法,我在顶部使用GuideLine
和ScrollView
创建了视差动画.
Creating a parallax effect
To test those methods I created a parallax animation using a GuideLine
and a ScrollView
on top.
在Activity
内的AndroidManifest.xml
上,添加以下内容:android:hardwareAccelerated="true"
.
On the AndroidManifest.xml
, inside your Activity
, add this: android:hardwareAccelerated="true"
.
<activity
android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:hardwareAccelerated="true">
...
MainActivity.java
Guideline guideTopInfo;
ConstraintLayout constraintLayout;
ConstraintLayout.LayoutParams params;
ConstraintSet constraintSet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
guideTopInfo = (Guideline) findViewById(R.id.guideline2);
constraintLayout = (ConstraintLayout) findViewById(R.id.constraint_root);
params = (ConstraintLayout.LayoutParams) guideTopInfo.getLayoutParams();
//constraintSet = new ConstraintSet();
//constraintSet.clone(constraintLayout);
final ScrollView scrollView = (ScrollView) findViewById(R.id.scroll_front);
scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
float percentage = scrollView.getScrollY() * 0.0001f; // 0.001f faster // 0.00001f slower parallax animation
Log.d("mLog", String.valueOf(percentage));
if(percentage >= 0) {
// my XML percentage value was 12%
params.guidePercent = 0.12f - percentage; // up
//params.guidePercent = 0.12f + percentage; // downm
guideTopInfo.setLayoutParams(params);
//constraintSet.setGuidelinePercent(R.id.guideline2, 0.12f - percentage);
//TransitionManager.beginDelayedTransition(constraintLayout);
//constraintSet.applyTo(constraintLayout);
}
}
});
}
关于视差的其他我的答案. ;)
My other answer about parallax if anyone is interested. ;)
这篇关于以编程方式更改约束布局中的准则百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!