【问题标题】:Change Guideline percentage in constraint layout programmatically以编程方式更改约束布局中的指南百分比
【发布时间】:2016-12-11 10:48:34
【问题描述】:

我有这样的约束布局指南

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

【问题讨论】:

    标签: android android-constraintlayout


    【解决方案1】:

    有两种方法:

    该方法从ConstraintLayout中获取特定参数并进行修改。

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

    此方法包括克隆ConstraintLayout 属性,修改它们,然后应用到View

    但是很慢。

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

    创建视差效果

    为了测试这些方法,我在顶部使用 GuideLineScrollView 创建了视差动画。

    AndroidManifest.xmlActivity 中,添加: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 关于视差。 ;)

    【讨论】:

    • 我尝试使用 Constraintset - 没有任何反应。你知道我可以从哪里开始搜索问题吗?
    • @kilianeller 我建议使用 ConstraitLayout.LayoutParams,因为 Constraintset 太慢了。无论如何,检查这些东西:1-你是否正确设置了id? 2- 当您在 XML 上手动更改指南时,视图会更改它的轴吗?
    • XML 中的更改指南按预期工作。 ConstraitLayout.LayoutParams 也可以按预期工作 - 我只是想使用 Constraintset 来制作简单的动画。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:
      guideline.setGuidelinePercent(value: Float)
      

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

      【讨论】:

      • 不知何故,这在运行时似乎对我不起作用
      【解决方案4】:

      你可以让它成为中心,我希望它对我有帮助

      <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" />
      

      【讨论】:

        【解决方案5】:

        您需要使用 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);
        

        【讨论】:

          【解决方案6】:

          在Kotlin & inside 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 布局中的指南元素:

           <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"  />
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-03-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多