Style.xml的妙用(一)

Style.xml之于Android犹如css之于Jsp。

如果只是想看到最终结论的、悟性高的,看到上面那句话就可以离开了。
现在,我来详细讲讲怎么用好Style.xml

妙用一

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/sensor"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

这样的布局文件是很正常的。但是不如这样好。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/all_match"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/sensor"
        style="@style/all_fill" />

</LinearLayout>

省时省力,一眼还能看出是什么布局方式。
只需要在Style.xml 中添加 这些代码即可。

    <style name="all_fill" >
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
    </style>
    <style name="all_wrap" >
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
    </style>
    <style name="width_fill" >
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
    </style>
    <style name="height_fill" >
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">fill_parent</item>
    </style>

可以提前写好自己的一个Style.xml文件,然后以后开发的时候直接把该文件中的这几行代码放入工程中,就可以方便快捷的使用了。
从“android:layout_width="fill_match" android:layout_height="fill_match"”变成了"style="@style/all_fill""。从67个字符变成了23个字符,从观看2个具体属性变成观看1个具体属性就可以知道布局。
非常的有效率啊!

To Be Continue

我是D,希望这篇文章对你有帮助

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容