android中styles的使用

本文详细介绍了如何在Android中定义自定义控件(MyCustomWidget),包括在attrs.xml中定义属性,如何在main.xml中引用并设置这些属性,以及如何使用styles.xml来简化属性配置过程。此外,还讲解了如何利用命名空间来区分不同控件属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值