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

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

问题描述

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.

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.

The code is

 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;
    }

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

解决方案

Everybody.

I was able to do it.

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.

Regards.

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

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