标签: declare-styleable

在Android中声明可设置的属性

关于declare-styleable标记的珍贵文档很少,我们可以通过它来声明组件的自定义样式.我确实找到了这个标签format属性的有效值列表attr.虽然这很好,但它没有解释如何使用其中的一些值.浏览attr.xml(标准属性的Android源代码),我发现你可以做以下事情:

<!-- The most prominent text color.  -->
<attr name="textColorPrimary" format="reference|color" />
Run Code Online (Sandbox Code Playgroud)

format属性显然可以设置为值的组合.据推测,该format属性有助于解析器解释实际的样式值.然后我在attr.xml中发现了这个:

<!-- Default text typeface. -->
<attr name="typeface">
    <enum name="normal" value="0" />
    <enum name="sans" value="1" />
    <enum name="serif" value="2" />
    <enum name="monospace" value="3" />
</attr>

<!-- Default text typeface style. -->
<attr name="textStyle">
    <flag name="normal" value="0" />
    <flag name="bold" value="1" />
    <flag name="italic" value="2" />
</attr>
Run Code Online (Sandbox Code Playgroud)

这两个似乎都声明了一组指定样式的允许值.

所以我有两个问题:

  1. 可以采用一组enum值之一的样式属性与可以采用一组值的样式属性之间的区别是什么flag? …

xml android styleable declare-styleable

77
推荐指数
2
解决办法
7万
查看次数

获取样式属性中使用的可绘制引用的资源ID

拥有此自定义视图MyView我定义了一些自定义属性:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyView">
        <attr name="normalColor" format="color"/>
        <attr name="backgroundBase" format="integer"/>
    </declare-styleable>   
</resources>
Run Code Online (Sandbox Code Playgroud)

并在布局XML中将它们分配如下:

    <com.example.test.MyView
        android:id="@+id/view1"
        android:text="@string/app_name"
        . . .
        app:backgroundBase="@drawable/logo1"
        app:normalColor="@color/blue"/>
Run Code Online (Sandbox Code Playgroud)

起初我以为我可以backgroundBase使用以下方法检索自定义属性:

TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyView, defStyle, 0);
int base = a.getInteger(R.styleable.MyView_backgroundBase, R.drawable.blank);
Run Code Online (Sandbox Code Playgroud)

仅在未分配属性且R.drawable.blank返回默认值时才有效.
app:backgroundBase赋值时抛出异常"无法转换为整数类型= 0xn",因为即使自定义属性格式将其声明为整数,它实际上引用了a Drawable,应该按如下方式检索:

Drawable base = a.getDrawable(R.styleable.MyView_backgroundBase);
if( base == null ) base = BitMapFactory.decodeResource(getResources(), R.drawable.blank);
Run Code Online (Sandbox Code Playgroud)

这很有效.
现在我的问题:
我真的不想Drawable从TypedArray中获取,我想要对应的整数id app:backgroundBase(在上面的示例中它将是 R.drawable.logo1).我怎么才能得到它?

android custom-controls declare-styleable android-resources

27
推荐指数
1
解决办法
2万
查看次数

声明风格和风格之间的区别

我已经开始玩我的Android应用程序中的样式等,到目前为止我已经完成了所有工作.我完全理解指南的"风格" 部分.

但是,环顾四周,就像在这个帖子中一样,我无法弄清楚两者之间的区别(declare-stylablestyle).从我的理解中declare-styleable获取其中指定的属性并将其指定为可样式,然后从代码中根据需要更改它.

但如果这是它真正的功能,那么在布局中定义属性会不会更简单?或者声明一个指定它的样式?

android styles declare-styleable

23
推荐指数
1
解决办法
2万
查看次数

如何为不同的标签声明几个具有相同名称的可设置属性?

我希望我的ViewA和ViewB都有"标题"标签.但我不能把它放进去attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="ViewA">
        <attr name="title" format="string" />
    </declare-styleable>
    <declare-styleable name="ViewB">
        <attr name="title" format="string" />
        <attr name="max" format="integer" />
    </declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)

因为错误属性"标题"已经定义.另一个问题显示了这个解

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="title" format="string" />
    <declare-styleable name="ViewB">
        <attr name="max" format="integer" />
    </declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)

但在那种情况下,R.styleable.ViewA_titleR.styleable.ViewB_title没有生成.我需要它们使用以下代码从AttributeSet读取属性:

TypedArray a=getContext().obtainStyledAttributes( as, R.styleable.ViewA);
String title = a.getString(R.styleable.ViewA_title);
Run Code Online (Sandbox Code Playgroud)

我怎么解决这个问题?

java xml android styleable declare-styleable

23
推荐指数
2
解决办法
7628
查看次数

错误:在<declare-styleable> MenuView中,无法找到属性android:preserveIconSpacing

我创建了一个应用程序,当试图运行时,得到这样的错误.

ERROR: In <declare-styleable> MenuView, unable to find attribute android:preserveIconSpacing
Run Code Online (Sandbox Code Playgroud)

问题 在此输入图像描述

在R.java结束时 在此输入图像描述

请帮我解决这个问题

android view declare-styleable

17
推荐指数
3
解决办法
3万
查看次数

使用样式和主题为自定义属性创建默认值

我有几个自定义View,我在其中创建了自定义的可样式属性,这些属性在xml布局中声明并在视图的构造函数中读入.我的问题是,如果我在xml中定义布局时没有为所有自定义属性提供显式值,我如何使用样式和主题来获得将传递给我View的构造函数的默认值?

例如:

attrs.xml:

<declare-styleable name="MyCustomView">
    <attr name="customAttribute" format="float" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

layout.xml(android:为简单起见,删除了标签):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.mypackage" >

    <-- Custom attribute defined, get 0.2 passed to constructor -->

    <com.mypackage.MyCustomView
        app:customAttribute="0.2" />

    <-- Custom attribute not defined, get a default (say 0.4) passed to constructor -->

    <com.mypackage.MyCustomView />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

android declare-styleable

15
推荐指数
1
解决办法
7405
查看次数

Android库项目使用declare-styleable - 如何编译?

我有一个自定义首选项控件,我已在values/attrs.xml中定义了一些属性.只是为了集中对话,这里是一个可以在values/attrs.xml中找到的属性的示例:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyCustomView">
        <attr name="android:text"/>
        <attr name="android:textColor"/>            
        <attr name="extraInformation" format="string" />
    </declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)

要使用这些属性,可以使用要在其中使用的xmlns标记,它看起来像这样:

xmlns:custom="http://schemas.android.com/apk/res/com.conundrum.app.lib"
Run Code Online (Sandbox Code Playgroud)

这就是问题所在:xmlns定义引用了LIBRARY的包名,这个资源在LIBRARY项目中编译得很好.但是,包含Library项目的Android项目具有不同的包名称,Android尝试合并所有资源.当它到达这个xmlns定义时,它会变得很糟糕,因为包含Android项目的包名称是不同的.

有没有人对在包含Android项目时仍然有效的库项目中使用xmlns引用有任何想法?

声明风格只是Android团队在考虑图书馆时的疏忽吗?

android declare-styleable

14
推荐指数
3
解决办法
5599
查看次数

如何在声明样式中定义整数数组?

我正在实现自己<declare-styleable>的自定义视图(按照此处的说明).我希望能够将整数数组指定为可能的XML属性之一.我如何能:

  1. 将整数数组指定为XML属性attrs.xml
  2. 在我的自定义视图中调用后,从TypedArray中获取它obtainStyledAttributes()

android android-xml typed-arrays declare-styleable android-resources

13
推荐指数
1
解决办法
5812
查看次数

有人可以在这个例子中解释我声明样式的XML标签及其使用背后的理论吗?

我正在阅读初学Android 4开发,在第5章中它讨论了GalleryImageVievs,并介绍了声明式样式的 XML标签,但没有解释其目的.我试图在参考上找到一些信息,没有运气..例如我们有以下内容:

RES /值/ attrs.xml

<?xml version=”1.0” encoding=”utf-8”?> 
<resources>
    <declare-styleable name=”Gallery1”>
        <attr name=”android:galleryItemBackground” />
    </declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)

example.java

public class GalleryActivity extends Activity {
[...]
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.main);
        Gallery gallery = (Gallery) findViewById(R.id.gallery1);
        gallery.setAdapter(new ImageAdapter(this)); 
        [...]
    }

    [...]

    public class ImageAdapter extends BaseAdapter {
        [...]
        int itemBackground;

        public ImageAdapter(Context c) {
            context = c;
            //---setting the style---
            TypedArray a = obtainStyledAttributes(
            R.styleable.Gallery1); 
            itemBackground = a.getResourceId(
                        R.styleable.Gallery1_android_galleryItemBackground, 0);
            a.recycle();
        }

        public View …
Run Code Online (Sandbox Code Playgroud)

android attr declare-styleable

9
推荐指数
1
解决办法
1万
查看次数

Android:如何从自定义视图的超类中获取属性

我有一个A具有TextView 的自定义视图.我做了一个返回resourceIDTextView的方法.如果未定义文本,则默认情况下该方法将返回-1.我还有一个B从视图继承的自定义视图A.我的自定义视图的文本为"hello".当我调用方法来获取超类的属性时,我得到了-1.

在代码中还有一个例子,我可以如何检索值,但感觉有点hacky.

attrs.xml

<declare-styleable name="A">
    <attr name="mainText" format="reference" />
</declare-styleable>

<declare-styleable name="B" parent="A">
    <attr name="subText" format="reference" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

A级

protected static final int UNDEFINED = -1;

protected void init(Context context, AttributeSet attrs, int defStyle)
{
     TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.A, defStyle, 0);

     int mainTextId = getMainTextId(a);

     a.recycle();

     if (mainTextId != UNDEFINED)
     {
        setMainText(mainTextId);
     }
}

protected int getMainTextId(TypedArray a)
{
  return a.getResourceId(R.styleable.A_mainText, UNDEFINED);
}
Run Code Online (Sandbox Code Playgroud)

B级

protected void init(Context context, AttributeSet attrs, …
Run Code Online (Sandbox Code Playgroud)

inheritance android android-custom-view declare-styleable android-attributes

8
推荐指数
1
解决办法
2853
查看次数