I can#39;t use API reflection on androidx.appcompat:appcompat:1.1.0(我不能在 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 反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:我不能在 androidx.appcompat:appcompat:1.1.0 上使用 API 反射
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01