RadioGroup显示不正确

时间:2017-05-15 03:42:45

标签: android radio-group

我在

使用了RadioButtonCenter类

https://github.com/pizza/MaterialTabs/blob/master/sample/src/io/karim/materialtabs/sample/ui/RadioButtonCenter.java

它在API 24下面运行良好。但在API 24中,它显示不正确。

当我点击新标签时。标签旧始终有效。当我打算活动差异和回来。它显示正确。

我没有发现问题。请。救救我!

这是xml

  <RadioGroup
    android:id="@+id/bottom_navigation_on_main"
    android:layout_width="match_parent"
    android:layout_height="@dimen/item_height"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal"
    android:weightSum="4">

    <RadioButtonCenter
        android:id="@+id/tab_home"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/selector_tab_main"
        android:button="@null"
        android:clickable="true"
        android:gravity="center"
        app:radioDrawable="@drawable/selector_tab_home" />

    <RadioButtonCenter
        android:id="@+id/tab_search"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:button="@null"
        android:clickable="true"
        android:background="@drawable/selector_tab_main"
        android:gravity="center"
        app:radioDrawable="@drawable/selector_tab_search" />
   </RadioGroup>

这是选择器

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_home_on" android:state_checked="true" 
 />
<item android:drawable="@drawable/ic_home_on" android:state_activated="true" 
 />
<item android:drawable="@drawable/ic_home_off" />
 </selector>

1 个答案:

答案 0 :(得分:0)

我发现了问题。我添加requestLayout();现在它奏效了!

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (buttonDrawable != null) {
        if (Build.VERSION.SDK_INT >= 24){
            requestLayout();
        }
        buttonDrawable.setState(getDrawableState());
        final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
        final int height = buttonDrawable.getIntrinsicHeight();

        int y = 0;

        switch (verticalGravity) {
            case Gravity.BOTTOM:
                y = getHeight() - height;
                break;
            case Gravity.CENTER_VERTICAL:
                y = (getHeight() - height) / 2;
                break;
        }

        int buttonWidth = buttonDrawable.getIntrinsicWidth();
        int buttonLeft = (getWidth() - buttonWidth) / 2;
        buttonDrawable.setBounds(buttonLeft, y, buttonLeft + buttonWidth, y + height);
        buttonDrawable.draw(canvas);
    }
}