http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 2218|回复: 5

[新手开发之旅] Android新手开发之旅-style的简单使用

[复制链接]
发表于 2018-12-6 16:36:02 | 显示全部楼层 |阅读模式
本帖最后由 liu 于 2018-12-6 17:32 编辑

style的简单使用




Android新手开发之旅【目录】






写代码时经常会有一些控件的属性一样,如果都去一个一个写的话不仅浪费时间还增加了代码量,这时我们就可以用到style,把相同的属性提取出来进行引用


用法很简单,以TextView举个例子:

实现下面界面:

         QQ截图20181206155301.png

在不使用style.xml的情况下代码:
[XML] 纯文本查看 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="10dp"
        android:text="开封"
        android:textColor="@android:color/black"
        android:textSize="18sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="郑州"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="10dp"
        android:textColor="@android:color/black"
        android:textSize="18sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="北京"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="10dp"
        android:textColor="@android:color/black"
        android:textSize="18sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="上海"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="10dp"
        android:textColor="@android:color/black"
        android:textSize="18sp" />
</LinearLayout>






使用style.xml的情况下:

activity_main:
[XML] 纯文本查看 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- style="@style/textview"中的textview就是styles.xml的所设置样式的名字 -->
    <TextView
        style="@style/textview"
        android:text="开封" />

    <TextView
        style="@style/textview"
        android:text="郑州" />

    <TextView
        style="@style/textview"
        android:text="北京" />

    <TextView
        style="@style/textview"
        android:text="上海" />
</LinearLayout>

res/styles.xml:
[XML] 纯文本查看 复制代码
<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <!-- 设置textview的样式 -->
    <style name="textview">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_marginLeft">15dp</item>
        <item name="android:layout_marginTop">10dp</item>
        <item name="android:textColor">@android:color/black</item>
        <item name="android:textSize">18sp</item>
    </style>
</resources>


对比一下会发现实现同一个界面在使用style的情况下少写了不少代码,既节省了我们的时间也减少了代码量,属性相同的控件越多效果越明显








1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2018-12-6 17:36:11 | 显示全部楼层
我只是路过打酱油的。
发表于 2018-12-6 17:37:42 | 显示全部楼层
我只是路过打酱油的。
发表于 2018-12-6 17:39:15 | 显示全部楼层
下次可以附个Demo 就更好了
发表于 2018-12-6 17:45:43 | 显示全部楼层
强烈支持楼主ing……
发表于 2018-12-7 17:23:13 | 显示全部楼层
感恩无私的分享与奉献
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-25 13:56

© 2014-2021

快速回复 返回顶部 返回列表