在没有setBounds的情况下,MaterialButton drawableLeft以编程方式不显示

时间:2019-04-14 02:52:05

标签: android xml-drawable

我有以下代码以编程方式设置drawableLeft

  public void updateActionButton() {
    int size = (int) (buttonAction.getTextSize() * 1.75);
    if (drawableNext == null) {
        drawableNext = ContextCompat.getDrawable(getActivity(), R.drawable.ic_arrow_right_white_48dp);
        drawableInfo = ContextCompat.getDrawable(getActivity(), R.drawable.ic_information_white_48dp);
        drawableNext.setBounds(0, 0, size, size);
        drawableInfo.setBounds(0, 0, size, size);
    }
    Drawable leftDrawable, rightDrawable;
    if (next) {
        buttonAction.setText(R.string.next);
        rightDrawable = drawableNext;
        leftDrawable = null;
    } else {
        leftDrawable = drawableInfo;
        rightDrawable = null;
    }
    buttonAction.setCompoundDrawables(leftDrawable, null, rightDrawable, null);
}

其中buttonAction是一个MaterialButton,并且正在设置此主题(如果我不使用它,则MaterialButton会使应用崩溃)

<style name="AppTheme" parent="Theme.MaterialComponents.Light">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowBackground">@color/colorBackground</item>
    <item name="android:windowEnterTransition">@android:transition/slide_right</item>
    <item name="android:windowExitTransition">@android:transition/slide_left</item>
</style>

这很好用。

但是没有手动设置边界,可绘制对象就不会出现...

如果我不设置界限,为什么drawable不出现? 而且,有没有办法无需手动设置大小?

0 个答案:

没有答案