在Android中的styles.xml中使用自定义attrs [英] Using custom attrs in styles.xml in android

查看:69
本文介绍了在Android中的styles.xml中使用自定义attrs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将自定义属性样式设置为

I made a custom attribute style as

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="EffectsTextView">        
        <attr name="strokeWidth" format="float" />
        <attr name="strokeMiter" format="float" />
        <attr name="strokeColor" format="color" />        
    </declare-styleable>
</resources>

在布局文件中,我可以使用此自定义属性为该属性分配名称空间:

In Layout file, I could use this custom attribute assigning a namespace for this attribute:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    
xmlns:effects="http://schemas.android.com/apk/res-auto/com.base.view.EffectsTextView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#40000000" >

 <com.base.view.EffectsTextView
        android:id="@+id/textView1"
        android:layout_width="290dip"
        android:layout_height="80dip"
        android:layout_marginLeft="5dip"
        android:layout_marginTop="16dip"
        android:gravity="center"
        android:text="SETTINGS"
        android:textSize="44dip"
        android:textStyle="bold"  
        effects:strokeWidth="10"  />    <--- custom tag "effects"

但是在styles.xml中无法识别此自定义标签

However it was not recognizing this custom tag in styles.xml

<style name="EffectsHeaderTextStyle">
     <item name="effects:strokeWidth">10</item>
</style>

错误:找不到与给定名称和效果匹配的资源,尽管我将名称空间与在RelativeLayout文件中放置的名称相同.

Error: No resource found that matches the given name, effects, though I put the namespace same as I did in RelativeLayout file.

有什么想法吗?谢谢

推荐答案

将XML更改为:

xmlns:effects="http://schemas.android.com/apk/res/com.base.view.EffectsTextView"

并更改您的样式:

<style name="EffectsHeaderTextStyle">
    <item name="strokeWidth">10</item>
</style>

我对字体做了类似的事情:

I did something similar with fonts:

https://github.com/androidfu/CodeExamples/tree/master/com.androidfu.CustomFontsDemo

这篇关于在Android中的styles.xml中使用自定义attrs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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