zoukankan      html  css  js  c++  java
  • 2-VIV-Android控件之res资源加载

    零、前言

    [1].本想在控件使用中穿插讲一下资源在加载,但感觉知识点挺多还是单开一篇专门讲一下吧。
    [2].values/string.xml的使用
    [3].values/dimens.xml的使用
    [4].values/colors.xml的使用
    [5].values/styles.xml的使用
    [6].选择器selector的使用
    [7].anim里xml动画加载


    一、values/string.xml的使用

    1.单字符串
    <string name="title">张风捷特烈</string>
    
    //使用单字符串
    setTitle(getResources().getString(R.string.title));
    //xml里
    android:text="@string/title"
    
    9414344-c7fde2ad1ed2a9a2.png
    定义string.png
    2.字符串数组
    <string-array name="coder">
        <item>Java</item>
        <item>Python</item>
        <item>JavaScript</item>
        <item>C++</item>
        <item>C</item>
    </string-array>
    
    //获取字符数组
    String[] coders = getResources().getStringArray(R.array.coder);
    for (int i = 0; i < coders.length; i++) {
        mIdTvCoder.append(coders[i]+"
    ");
    }
    
    9414344-28c302feddf6a06f.png
    加载数组.png

    二、values/dimens.xml的使用

    <dimen name="sp_18">18sp</dimen>
    
    //xml
    android:textSize="@dimen/sp_18"
    //获取尺寸==已转换为px
    float dimension = getResources().getDimensionPixelOffset(R.dimen.sp_18);
    mIdTvCoder.setTextSize(TypedValue.COMPLEX_UNIT_PX,dimension);
    

    三、values/styles.xml的使用

    用来抽取一些常用样式

    <style name="SmallBlueTextView">
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:textSize">@dimen/sp_12</item>
        <item name="android:textColor">@dimen/sp_12</item>
    </style>
    

    使用

        <TextView
            android:id="@+id/id_tv_coder"
            style="@style/SmallBlueTextView"/>
    

    四、values/colors.xml的使用

    <color name="gray">#eeeeee</color>
    
    //获取颜色
    int color = getResources().getColor(R.color.gray);
    mIdTvCoder.setBackgroundColor(color);
    //xml里
    android:background="@color/gray"
    

    9414344-53855a6fb939895e.png
    颜色.png

    五、选择器selector的使用

    1.drawable文件夹下的selector:drawable/sel_db_gary_white.xml

    颜色部分都可以改成相应的图片资源,这里就不赘述了

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <!--按压为灰色-->
        <item android:drawable="@color/gray" android:state_pressed="true"/>
        <!--不可用状态为红色-->
        <item android:drawable="@color/red" android:state_enabled="false"/>
        <!--默认白色-->
        <item android:drawable="@color/white"/>
    </selector>
    

    当做一个drawable使用

     android:background="@drawable/sel_db_gary_white"
    

    常见属性

      1、android:drawable 可绘制对象资源。
      2、android:state_pressed 按下
      3、android:state_checked 选中
       4、android:state_checkable 可选中
      5、android:state_selected 方向键浏览列表
      6、android:state_enabled 可用
        7、android:state_focused 获取焦点
      8、android:state_activated 激活
      9、android:state_hovered 光标悬停
      10、android:state_window_focused 窗口有焦点
    
    2.color文件夹下的selector:color/sel_col_gary_white.xml

    注意小坑,只能给文字用,背景一用就崩了!!!
    在res文件夹下新建color文件夹

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="#A6A6A6" android:state_enabled="false"/>
        <item android:color="#A6D3CF" android:state_pressed="true"/>
        <item android:color="@color/blue"/>
    </selector>
    

    xml中当作颜色使用

    android:textColor="@color/sel_col_gary_white"
    

    代码中使用

    //获取选择器
    ColorStateList selColor = getResources().getColorStateList(R.color.sel_col_gary_white);
    mIdTvCoder.setTextColor(selColor);
    

    六、anim里xml动画加载

    定义

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <rotate
            android:duration="2000"
            android:fromDegrees="0"
            android:pivotX="50%"
            android:pivotY="50%"
            android:repeatMode="reverse"
            android:toDegrees="360"
        />
    </set>
    

    使用

    Animation rotate = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);
    mIdTvCoder.setAnimation(rotate);
    mIdTvCoder.startAnimation(rotate);
    

    后记、

    1.声明:

    [1]本文由张风捷特烈原创,转载请注明
    [2]欢迎广大编程爱好者共同交流
    [3]个人能力有限,如有不正之处欢迎大家批评指证,必定虚心改正
    [4]你的喜欢与支持将是我最大的动力

    2.连接传送门:

    更多安卓技术欢迎访问:安卓技术栈
    我的github地址:欢迎star
    简书首发,腾讯云+社区同步更新
    张风捷特烈个人网站,编程笔记请访问:http://www.toly1994.com

    3.联系我

    QQ:1981462002
    邮箱:1981462002@qq.com
    微信:zdl1994328

    4.欢迎关注我的微信公众号,最新精彩文章,及时送达:
    9414344-c474349cd3bd4b82.jpg
    公众号.jpg
  • 相关阅读:
    HDU 3389 Game (阶梯博弈)
    国内操作系统OS分析(上)
    激光雷达应用技术分析
    构建深度学习框架运行平台
    GitHub上YOLOv5开源代码的训练数据定义
    GitHub上开源的YOLOv5
    深度学习调用TensorFlow、PyTorch等框架
    CUDA C 纹理提取Texture Fetching
    CPU,GPU,GPGPU
    毫米波RADAR与LIDAR探秘
  • 原文地址:https://www.cnblogs.com/toly-top/p/9781907.html
Copyright © 2011-2022 走看看