首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用动画更改RadioButtons的权重

使用动画更改RadioButtons的权重
EN

Stack Overflow用户
提问于 2020-04-08 17:10:03
回答 1查看 148关注 0票数 0

我有下面的代码。我想在用户单击此按钮时更改单选按钮的权重。我的意思是,当用户选择一个单选按钮时,将其权重更改为2,而未选中的单选按钮的权重更改为1,反之亦然。(单选按钮组的权重和为3)。当我选择左侧单选按钮时,首先更改大小,然后更改颜色。我希望当用户选择左单选按钮时,它会从左向右按下,而不是突然改变大小。

XML代码:

代码语言:javascript
运行
复制
<RadioGroup
        android:checkedButton="@+id/onTimeRadioButton"
        android:id="@+id/checkToggle"
        android:layout_marginTop="15dp"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:background="@drawable/rb_drawable"
        android:orientation="horizontal"
        android:weightSum="3">

        <RadioButton
            android:layout_marginTop="1dp"
            android:layout_marginBottom="1dp"
            android:layout_marginLeft="1dp"
            android:id="@+id/groupRadioButton"
            android:background="@drawable/toggle_widget_background"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:onClick="onRadioBttonClicked"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:text="groupCheck"
            android:textSize="15sp"
            android:textAlignment="center"
            android:textColor="@android:color/white" />

        <RadioButton
            android:layout_marginRight="1dp"
            android:layout_marginTop="1dp"
            android:layout_marginBottom="1dp"
            android:onClick="onRadioBttonClicked"
            android:id="@+id/onTimeRadioButton"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:textAlignment="center"
            android:background="@drawable/toggle_widget_background"
            android:button="@null"
            android:textSize="15sp"
            android:gravity="center"
            android:text="onTimeCheck"
            android:textColor="@android:color/white" />
    </RadioGroup>

Java代码:

代码语言:javascript
运行
复制
 public void onRadioBttonClicked(View view){
    boolean checked = ((RadioButton) view).isChecked();
    switch (view.getId()){
        case R.id.groupRadioButton:
            if(checked){
                ViewWeightAnimationWrapper animationWrapper = new ViewWeightAnimationWrapper(groupRadioBtn);
                ObjectAnimator anim = ObjectAnimator.ofFloat(animationWrapper,
                        "weight",
                        animationWrapper.getWeight(),
                        2);
                anim.setDuration(500);
                anim.start();
                ViewWeightAnimationWrapper animationWrapper2 = new ViewWeightAnimationWrapper(onTimeRadioBtn);
                animationWrapper2.getWeight();
                animationWrapper2.setWeight(1);
            }
            break;
        case R.id.onTimeRadioButton:
            if(checked){
                if(checked){
                    ViewWeightAnimationWrapper animationWrapper = new ViewWeightAnimationWrapper(onTimeRadioBtn);
                    ObjectAnimator anim = ObjectAnimator.ofFloat(animationWrapper,
                            "weight",
                            animationWrapper.getWeight(),
                            2);
                    anim.setDuration(500);
                    anim.start();
                    ViewWeightAnimationWrapper animationWrapper2 = new ViewWeightAnimationWrapper(groupRadioBtn);
                    animationWrapper2.setWeight(1);
                }
            }
            break;
    }
}
 public class ViewWeightAnimationWrapper {
    private View view;

    public ViewWeightAnimationWrapper(View view) {
        if (view.getLayoutParams() instanceof LinearLayout.LayoutParams) {
            this.view = view;
        } else {
            throw new IllegalArgumentException("The view should have LinearLayout as parent");
        }
    }

    public void setWeight(float weight) {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
        params.weight = weight;
        view.getParent().requestLayout();
    }

    public float getWeight() {
        float test=((LinearLayout.LayoutParams) view.getLayoutParams()).weight;
        return test;
    }
}

EN

回答 1

Stack Overflow用户

发布于 2020-04-08 17:16:44

我建议在LinearLayout.LayoutParams中程序化地改变权重,在那之前添加一行:

代码语言:javascript
运行
复制
 TransitionManager.beginDelayedTransition(yourRootLinearLayout, new ChangeBounds());
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61097128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档