wcm688 2013-11-11 09:22 采纳率: 0%
浏览 1042

android 自定义属性,其中有个属性是引用图片,但实际没有引用到?求解

布局文件其中一段:
<com.snd.test.view.CustomBottom
android:id="@id/bottom_home_btn"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
bdm:img="@drawable/tabbar_icon_home_bg"
bdm:msg="@string/guangjie"

/>

attrs.xml






自定义的view:

private ImageView icon;
private TextView itext;
public CustomBottom(Context context){
this(context,null);
}

public CustomBottom(Context context, AttributeSet attrs) {
super(context, attrs);
setGravity(Gravity.CENTER_HORIZONTAL);
this.init(context);

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.btn);
String msg = a.getString(R.styleable.btn_msg);
if (null != msg ){
this.itext.setText(msg);
}

this.icon.setBackgroundResource(a.getResourceId(R.styleable.btn_img, 0));
a.recycle();
}
private void init(Context context){
LayoutInflater.from(context).inflate(R.layout.sdk_bottom_btn, this);
this.icon = (ImageView)this.findViewById(R.id.bottom_btn_ico);
this.itext = (TextView)this.findViewById(R.id.bottom_btn_text);
}

问题:能显示bdm:msg="@string/guangjie",但是这个bdm:img="@drawable/tabbar_icon_home_bg"不起效果,求解?

  • 写回答

1条回答 默认 最新

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2024-03-20 23:24
    关注

    所有内容均由人工智能模型输出,其内容的准确性和完整性无法保证,不代表我的态度或观点。
    GLM-4在2024.02.27SuperCLUE基准得分榜单中排行第四https://www.superclueai.com/


    该回答引用GLM-4,仅提供参考,不保证完全正确
    目前为GLM-4(智谱清言)专栏,网页版本:未知(更新时间未知)
    如有图像,GLM-4将尝试识别您的图像
    首先,检查你的attrs.xml文件是否正确定义了img属性,例如:

    <resources>
        <declare-styleable name="btn">
            <attr name="img" format="reference" />
            <attr name="msg" format="string" />
        </declare-styleable>
    </resources>
    

    确保img属性的格式设置为reference,这意味着它将引用一个资源ID。


    其次,在自定义View的构造函数中,确保正确地获取了img属性的值,并设置了背景。你的代码中有一个小错误,应该是R.styleable.btn_img而不是R.styleable.btn_img

    this.icon.setBackgroundResource(a.getResourceId(R.styleable.btn_img, 0));
    

    最后,检查你的布局文件是否正确引用了img属性,例如:

    <com.snd.test.view.CustomBottom
        android:id="@id/bottom_home_btn"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        bdm:img="@drawable/tabbar_icon_home_bg"
        bdm:msg="@string/guangjie" />
    

    确保bdm:img属性的值是一个有效的drawable资源ID。


    如果以上步骤都正确,但仍然不起效果,请尝试以下方法:

    1. 清理并重新构建项目。
    2. 检查是否有其他地方覆盖了icon的背景。
    3. init方法中,确保inflate的布局文件中包含了ImageView的ID bottom_btn_ico
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题