其他分享
首页 > 其他分享> > Android属性,声明样式,引用

Android属性,声明样式,引用

作者:互联网

我是android开发的新手,我正在尝试创建我的自定义视图.我遇到了很多问题.我已经解决了其中一些问题,但最难理解的是属性.
我们来看一下带有属性的示例xml文件

<declare-styleable name="ViewPagerIndicator">
        <!-- Style of the circle indicator. -->
        <attr name="vpiCirclePageIndicatorStyle" format="reference"/>
        <!-- Style of the icon indicator's views. -->
        <attr name="vpiIconPageIndicatorStyle" format="reference"/>
        <!-- Style of the line indicator. -->
        <attr name="vpiLinePageIndicatorStyle" format="reference"/>
        <!-- Style of the title indicator. -->
        <attr name="vpiTitlePageIndicatorStyle" format="reference"/>
        <!-- Style of the tab indicator's tabs. -->
        <attr name="vpiTabPageIndicatorStyle" format="reference"/>
        <!-- Style of the underline indicator. -->
        <attr name="vpiUnderlinePageIndicatorStyle" format="reference"/>
    </declare-styleable>
    <attr name="centered" format="boolean" />
    <attr name="selectedColor" format="color" />
    <attr name="strokeWidth" format="dimension" />
    <attr name="unselectedColor" format="color" />

    <declare-styleable name="CirclePageIndicator">
        <attr name="centered" />
        <attr name="fillColor" format="color" />
        <attr name="pageColor" format="color" />
        <attr name="android:orientation"/>
        <attr name="radius" format="dimension" />
        <attr name="snap" format="boolean" />
        <attr name="strokeColor" format="color" />
        <attr name="strokeWidth" />
        <attr name="android:background"/>
    </declare-styleable>
<declare-styleable name="LinePageIndicator">
    <attr name="centered" />
    <attr name="unselectedColor" />
    <attr name="selectedColor" />
    <attr name="lineWidth" format="dimension" />
    <attr name="strokeWidth" />
    <attr name="gapWidth" format="dimension" />
    <attr name="android:background"/>
</declare-styleable>

>参考格式是什么意思,我还不明白这种格式是什么意思,我发现它可以用于drawables我也看到它可以用于主题.请举例说明它有用的地方以及这种格式类型的主要目的是什么.在前面的示例中,它用于样式,但我不明白如何在我将使用自定义视图的xml文件中使用它.

06001

>每个declare-styleable name =“自定义视图名称”行,我可以理解,用于单独的自定义视图,我们可以在示例中看到这一点.但是< declare-styleable name =“ViewPagerIndicator”>的目的是什么,我没有在这个lib中找到任何具有这样名称的视图,这个样式包含我的第一个问题(引用属性).
>如果声明了declare-stylable属性,则意味着它在多个视图中,而不是在每个视图中重复代码,而是将公共属性放在外面.我对吗 ?

请帮助,我花了一天时间试图找到任何教程或文档.官方文件很差.我想很多新手都会对解释这个话题表示感谢.

请帮助理解这一点,最重要的问题是关于引用(第一次qustion)< attr name =“vpiCirclePageIndicatorStyle”format =“reference”/>这是什么,如何使用它,它被声明的地方?

另外我发现了另一个lib中使用的另一个例子

<declare-styleable name="Themes">
    <attr name="SliderStyle" format="reference"/>
    <attr name="PagerIndicatorStyle" format="reference"/>
</declare-styleable>

我根本无法理解这一点.请帮忙,你是我最后的希望.

解决方法:

AFAIK,reference允许您引用该属性中的其他drawable和样式.

<declare-styleable name="Theme">
    <attr name="indicatorStyle" format="reference"/>
</declare-styleable>

这允许我声明一个我以后可以获得的自定义样式

<style name="AppTheme.Platform.NoActionBar" parent="Theme.AppCompat.NoActionBar">
    <item name="indicatorStyle">@style/ViewPagerIndicator</item>
</style>

现在我可以在XML中使用它了

style="?attr/indicatorStyle"

如果我尝试在未声明它的主题中使用此属性,我将收到错误.这就是ViewPagerIndicator样式的用途,用于在app主题中声明自定义属性.我没有得到你的第三个问题,但这应该涵盖前两个问题.

标签:android,android-attributes,xml,android-layout,android-view
来源: https://codeday.me/bug/20190829/1763973.html