简体   繁体   English

通过Android Preference以编程方式设置图标(@ + android:id / icon)的大小

[英]Programatically setting size of icon (@+android:id/icon) from Android Preference

I subclassed an Android Preference to load my own layout (very slightly modified), but my layout still has the built-in icon provided by the Preference class: 我将Android偏好设置子类化以加载自己的布局(略有修改),但是我的布局仍然具有Preference类提供的内置图标:

<ImageView
    android:id="@+android:id/icon"
    android:layout_width="20dp"
    android:layout_height="20dp/>

I can't retrieve a reference to the ImageView programatically like the Android Preference class does because com.android.internal.R cannot be resolved: 我无法像Android Preference类一样以编程方式检索对ImageView的引用,因为com.android.internal.R无法解析:

ImageView imageView = (ImageView) view.findViewById(com.android.internal.R.id.icon);

Is it possible to programmatically change the size. 是否可以通过编程方式更改大小。 Ideally, I wouldn't create various layouts for each size I want. 理想情况下,我不会为所需的每种尺寸创建各种布局。 I think the solution here is to pass in an AttributeSet to my constructor and have it set the size of the icon, but I can't figure out how to do this. 我认为这里的解决方案是将AttributeSet传递给我的构造函数,并让它设置图标的大小,但是我不知道该怎么做。

Your ImageView id declaration is wrong in xml correct it : 您的ImageView id声明在xml中是错误的,请更正它:

android:id="@android:id/icon"

Initialize ImageView this way in activity : 在活动中以这种方式初始化ImageView:

ImageView imageView = (ImageView) view.findViewById(android.R.id.icon);

更改为@android:id/icon并导入android.R.id.icon

Try this solution: 试试这个解决方案:

imageView.post(new Runnable() {
@Override
public void run() {
imageView.setHeight(VALUE)
imageView.setWidth(VALUE)
}
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM