`
kanwoerzi
  • 浏览: 1647854 次
文章分类
社区版块
存档分类
最新评论

android中styles的使用

 
阅读更多
attrs.xml定义属性:<wbr style="line-height:25px"><span style="line-height:25px; height:10px; color:rgb(255,255,255); padding-top:1px; padding-right:1px; padding-bottom:1px; padding-left:1px"><br style="line-height:25px"></span><wbr style="line-height:25px">如果我自己定义控件MyCustomWidget,在main.xml里,我要使用这个控件,并且设置属性android:textSize=这样东西如何设置呢?<br style="line-height:25px"> 那这个属性从哪来的呢?需要设置attrs.xml,在values目录下创建这样个文件,然后内容如此:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">&lt;resources&gt;<br style="line-height:25px"> &lt;declare-styleablename="MyCustomView"&gt;<br style="line-height:25px"> &lt;attrname="text"format="string"/&gt;<br style="line-height:25px"> &lt;attrname="textColor"format="color"/&gt;<br style="line-height:25px"> Qisdachangertel:6029writethepaper.<br style="line-height:25px"> &lt;attrname="textSize"format="dimension"/&gt;<br style="line-height:25px"> &lt;/declare-styleable&gt;<br style="line-height:25px"> &lt;/resources&gt;</span><br style="line-height:25px"> 这个东西定义了一些属性名的取值类型是什么?<br style="line-height:25px"> 哎,既然有了这个属性类型,那我们就可以在layout里设置属性了.要在<br style="line-height:25px"><span style="color:#ff6600; line-height:25px"></span><span style="color:#993300; line-height:25px">xmlns:android="http://schemas.android.com/apk/res/</span><span style="color:#ff6600; line-height:25px">android</span><span style="color:#993300; line-height:25px">"</span><br style="line-height:25px"> 下面加一句.<br style="line-height:25px"><span style="color:#993300; line-height:25px">xmlns:myview="http://schemas.android.com/apk/res/</span><span style="color:#ff6600; line-height:25px">com.ui</span><span style="color:#993300; line-height:25px">"</span><br style="line-height:25px"> myview是命名空间,可以随便起名字.最后<span style="color:#ff6600; line-height:25px">com.ui</span>是声明控件属性的包的名字.<br style="line-height:25px"> 有了这个东西,应该明白为什么控件属性都设置成android:了.<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">&lt;com.ui.MyCustomView<br style="line-height:25px"> android:layout_width="fill_parent"<br style="line-height:25px"> android:layout_height="wrap_content"<br style="line-height:25px"> Qisdachangerwritethepaper.<br style="line-height:25px"></span><span style="color:#ff6600; line-height:25px">myview:text</span><span style="color:#0000ff; line-height:25px">="@string/app_name"</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">myview:textSize="32dp"</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">myview:textColor="@drawable/yellow"/></span><br style="line-height:25px"> 这样就可以设置属性了.<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">styles的使用:</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#993300; line-height:25px">styles.xml</span><span style="color:#003366; line-height:25px">用于定义一些属性值的集合。</span>格式如此.<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">&lt;?xmlversion="1.0"encoding="utf-8"?&gt;<br style="line-height:25px"> &lt;resources&gt;<br style="line-height:25px"> &lt;stylename="MyCustomView"&gt;<br style="line-height:25px"> &lt;itemname="textColor"&gt;#FFFF0000&lt;/item&gt;<br style="line-height:25px"> Qisdachangerwritethepaper.<br style="line-height:25px"> &lt;itemname="textSize"&gt;60dp&lt;/item&gt;<br style="line-height:25px"> &lt;/style&gt;<br style="line-height:25px"> &lt;/resources&gt;</span><br style="line-height:25px"><span style="line-height:25px; color:rgb(153,51,0)">一个styles</span>的本质就是一些属性值的集合。 <div style="line-height:25px">这样我们可以在layout中通过style的名字引用style。比如style="<span style="color:#993300; line-height:25px">@style/MyCustomView</span>"这样就可以了.</div> </wbr></wbr></wbr>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics