【问题标题】:RadioGroup not display correctRadioGroup 显示不正确
【发布时间】:2017-05-15 03:42:45
【问题描述】:

我在

使用了 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>

【问题讨论】:

  • 您的问题不清楚和简洁,请添加更多详细信息并详细说明并分享您的尝试。
  • 我更新了我的问题

标签: android radio-group


【解决方案1】:

我发现了问题。我添加 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);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    • 2020-10-30
    • 2018-06-11
    • 2015-05-06
    • 2012-09-02
    • 2013-07-05
    相关资源
    最近更新 更多