3 xml android android-layout android-view android-attributes
我是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>
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)<com.viewpagerindicator.CirclePageIndicator android:id="@+id/indicator" android:padding="10dip" android:layout_height="wrap_content" android:layout_width="wrap_content" custom:auto_cycle="true" custom:pageColor="@color/abc_background_cache_hint_selector_material_dark" />
declare-styleable name="Name of custom view"我可以理解,每一行用于单独的自定义视图,我们可以在示例中看到这一点.但是目的是什么<declare-styleable name="ViewPagerIndicator">,我没有在这个lib中找到任何具有这样名称的视图,这个样式包括我的第一个问题(引用属性).
如果声明了declare-stylable属性,则意味着它在多个视图中,并且不在每个视图中复制代码,而是将公共属性放在外部.我对吗 ?
请帮助,我花了一天时间试图找到任何教程或文档.官方文件很差.我想很多新手都会对解释这个话题表示感谢.
请帮助理解这一点,最重要的问题是关于引用(第一次问题) <attr name="vpiCirclePageIndicatorStyle" format="reference"/>这是什么,如何使用它,它被声明的地方?
另外我发现了另一个lib中使用的另一个例子
<declare-styleable name="Themes">
<attr name="SliderStyle" format="reference"/>
<attr name="PagerIndicatorStyle" format="reference"/>
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
我根本无法理解这一点.请帮忙,你是我最后的希望.
AFAIK,reference允许您引用该属性中的其他drawable和样式.
<declare-styleable name="Theme">
<attr name="indicatorStyle" format="reference"/>
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
这允许我声明一个我以后可以获得的自定义样式
<style name="AppTheme.Platform.NoActionBar" parent="Theme.AppCompat.NoActionBar">
<item name="indicatorStyle">@style/ViewPagerIndicator</item>
</style>
Run Code Online (Sandbox Code Playgroud)
现在我可以在XML中使用它了
style="?attr/indicatorStyle"
Run Code Online (Sandbox Code Playgroud)
如果我尝试在未声明它的主题中使用此属性,我将收到错误.这就是ViewPagerIndicator样式,在app主题中声明自定义属性.我没有得到你的第三个问题,但这应该涵盖前两个问题.
| 归档时间: |
|
| 查看次数: |
3453 次 |
| 最近记录: |