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

发布于 2020-04-08 17:16:44
我建议在LinearLayout.LayoutParams中程序化地改变权重,在那之前添加一行:
TransitionManager.beginDelayedTransition(yourRootLinearLayout, new ChangeBounds());https://stackoverflow.com/questions/61097128
复制相似问题