我无法在androidx.appcompat:appcompat:1.1.0上使用API​​反射 [英] I can't use API reflection on androidx.appcompat:appcompat:1.1.0

查看:307
本文介绍了我无法在androidx.appcompat:appcompat:1.1.0上使用API​​反射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对androidx.appcompat:appcompat:1.1.0有问题.这是一个新问题,因为在androidx.appcompat:appcompat:1.0.2上不存在.

I have a problem with androidx.appcompat:appcompat:1.1.0. It's a new problem because on androidx.appcompat:appcompat:1.0.2 it does not exist.

我有一个使用反射从微调器获取mPopup Field并设置其高度的代码.在appcompat:1.0.2上效果很好,但在androidx.appcomppat:appcompat:1.1.0上效果不佳.

I have a code that uses reflection to get mPopup Field from the spinner and set its height. It works very well on appcompat:1.0.2 but not on androidx.appcomppat:appcompat:1.1.0.

代码是

 private void setPopUp() {

        try {
            Field popup = getPopupField();

            // Get private mPopup member variable and try cast to ListPopupWindow
            final android.widget.ListPopupWindow popupWindow = (android.widget.ListPopupWindow) popup.get(spinner);

            // Set popupWindow height to max - 40dp
            spinner.post(new Runnable() {
                @Override
                public void run() {
                    Rect r = new Rect();
                    spinner.getGlobalVisibleRect(r);
                    int height = 200;
                    popupWindow.setHeight(height);
                }
            });
        } catch (NoClassDefFoundError | ClassCastException | IllegalAccessException e) {
            // silently fail...
        }


    }

    private static Field getPopupField () {
        if (sPopupField == null) {
            try {
                Field popup = Spinner.class.getDeclaredField("mPopup");
                popup.setAccessible(true);

                sPopupField = popup;
            } catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException e) {
                // silently fail...
            }
        }
        return sPopupField;
    }

我从appcompat:1.1.0阅读了有关新appcompatActivity的错误的信息.但是,我找不到解决问题的方法.

I read about bugs on a new appcompatActivity from appcompat:1.1.0. However, I can't find a solution to my problem.

推荐答案

所有人.

我能够做到.

首先,我使用Java代码将微调框放在活动上.以编程方式".

First, i put the spinner on the activity with java code. "programatically".

     public void initTest(){
    spinner2 = new Spinner(this, Spinner.MODE_DROPDOWN);
    spinner2.setAdapter(new ArrayAdapter(this, R.layout.spinner_item, datos));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        spinner2.setId(spinner2.generateViewId());
    }

    ConstraintLayout parentLayout = (ConstraintLayout)findViewById(R.id.main_activity);
    parentLayout.addView(spinner2, 0);

    ConstraintSet cs = new ConstraintSet();
    cs.clone(parentLayout);
    cs.setHorizontalBias(spinner2.getId(), 0.473F);
    cs.setVerticalBias(spinner2.getId(), 0.484F);
    cs.connect(spinner2.getId(), ConstraintSet.BOTTOM, parentLayout.getId(),ConstraintSet.BOTTOM);
    cs.connect(spinner2.getId(), ConstraintSet.START, parentLayout.getId(),ConstraintSet.START);
    cs.connect(spinner2.getId(), ConstraintSet.TOP, parentLayout.getId(),ConstraintSet.TOP);
    cs.connect(spinner2.getId(), ConstraintSet.END, parentLayout.getId(),ConstraintSet.END);
    // cs view id, else getId() returns -1

    // connect start and end point of views, in this case top of child to top of parent.
    // ... similarly add other constraints
    cs.applyTo(parentLayout);

}  

然后我调用问题开头的代码.

Then i call the code put at the beginning in my question.

我希望它适用于许多人,并跳过新版本中的错误.

I hope it works for many people, and to skip this error of the new version.

致谢.

这篇关于我无法在androidx.appcompat:appcompat:1.1.0上使用API​​反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆