以编程方式更改约束布局中的指南百分比

Change Guideline percentage in constraint layout programmatically

我有这样的约束布局指南

<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 值更改为其他值。我怎样才能做到这一点。

此代码会将您的 guidePercent 更改为 0

ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) guideline.getLayoutParams();
lp.guidePercent = 0;
guideline.setLayoutParams(lp);

您需要使用 ConstraintSet 对象在代码中设置 layout_constraintGuide_percent。

下面是在代码中设置layout_constraintGuide_percent的代码。首先需要创建约束集对象,调用clone方法传递constraintlayout,然后调用setGuidelinePercent传递guideline id和ratio。

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

    constraintSet.setGuidelinePercent(R.id.guideline, 0.4f);

有两种方法:

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);

This method consists in cloning the ConstraintLayout attributes, modifying them and then applying to the View.

BUT it's very slow.

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 创建了一个视差动画。

AndroidManifest.xml 上,在您的 Activity 中添加: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);
            }
        }
    });
}

我的另一个 answer 关于视差,如果有人感兴趣的话。 ;)

在Kotlin&Fragment或者其他activity里面,设置GuideLine app:相关参数有点难度,我是这样解决的:

 val view = inflate(this, R.layout.ly_custom_menu_item_wallet_side_menu, null)
 val guideL = view.findViewById<Guideline>(R.id.guideline_wallet)
          var constParam: ConstraintLayout.LayoutParams = guideL.layoutParams as ConstraintLayout.LayoutParams
          constParam.guidePercent = 0.42f
          guideL.layoutParams = constParam

这是我们 ly_custom_menu_item_wallet_side_menu.xml 布局中的 Guideline 元素:

 <androidx.constraintlayout.widget.Guideline 
    android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:id="@+id/guideline_wallet"
    android:orientation="vertical" 
   app:layout_constraintGuide_end="84dp"  />
guideline.setGuidelinePercent(value: Float)

这足以在运行时更改指南百分比。

你让它成为中心,和我一起工作,我希望它能有所帮助

<androidx.constraintlayout.widget.Guideline
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintGuide_percent="0.5"
                    app:layout_constraintStart_toStartOf="parent" />