加入收藏 | 设为首页 | 会员中心 | 我要投稿 东莞站长网 (https://www.0769zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

Android :Style的实施

发布时间:2021-11-25 19:59:20 所属栏目:教程 来源:互联网
导读:Style中文应该是式样,可以使用它指定View的外观,程序的主题等。下面是示例: ?xml version=1.0 encoding=utf-8? LinearLayout xmlns:Android=http://schemas.android.com/apk/res/android android:orientation=vertical android:layout_width=fill_parent a
Style中文应该是式样,可以使用它指定View的外观,程序的主题等。下面是示例:
 
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >  
<Button  
    android:background="#ff00ffff"  
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content"   
    android:textColor="#ffff0000"  
    android:textSize="18sp"  
    android:text="Hello android"  
    />  
</LinearLayout>  
本来我们定义一个View对象是以这种方式进行的,现在我们应用Style。
 
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    >  
<Button  
    style="@style/buttonstyle"  
    android:text="Hello android"  
    />  
</LinearLayout>  
在res/velues目录下面新建一个style.xml的文件。文件名是任意的,不过必须要以.xml结尾。
 
<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <style name="buttonstyle" parent="@android:style/TextAppearance.Medium">  
        <item name="android:background">#ff00ffff</item>  
        <item name="android:layout_width">fill_parent</item>  
        <item name="android:layout_height">wrap_content</item>  
        <item name="android:textColor">#ff00ffff</item>  
        <item name="android:textSize">18sp</item>  
    </style>  
</resources>  
这两种定义Button的方式实现的效果是一样的。经过比较就可以知道Style'的使用方法。需要注意的是parent 指定了式样的继承。Android预定义了一些式样,通过合理的继承可以减轻我们的工作。上面的方式是继承Android为我们提供的式样,要继承自己定义的式样,方法稍有不同。
 
<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <style name="MyButton.buttonstyle">  
     .............  
    </style>  
</resources>  
它说明buttonstyle继承于MyButton式样,而MyButton是一个自己定义的式样。这种方式是可以递归的。你可以另外定义一个substyle式样让他继承自buttonstyle,那么就是MyButton.buttonstyle.substyle了。
 
tips:我们为一个View指定了style,那么它会应用这个style。如果我们为一个ViewGroup指定了式样,那么它的Child View是不会继承它的style的。想要让所有的视图都继承一个Style,那就要用到主题Theme了(这句话虽然源自文档,但是强烈质疑这种做法的有效性)。

(编辑:东莞站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读