Android Android应用资源 | 学习笔记

一、字符串资源

  1. 为了改善程序的可读性维护成本,Android允许把应用中用到的各种资源:字符串资源颜色资源数据资源等集中放到res目录中定义,应用程序则可以直接使用这些资源定义的值。
  2. 字符串资源文件位于res/values目录下,根元素是< resources >< /resources >标记,在该元素中,使用< string >< /string >标记定义各字符串,其中,通过为< string >< /string >标记设置name属性来指定字符串的名称,在其实标记< string >和< /string >中间添加字符串内容。

在这里插入图片描述
提示使用用户名资源来完成。

步骤

在strings.xml文件中补充——
在这里插入图片描述
在activity_main.xml中加入——
在这里插入图片描述
完整的修改情况如下——
strings.xml:

<resources>
    <string name="app_name">My Application</string>
    <string name="user">用户名</string>
    <string name="password">密码</string>
    <string name="login">登录</string>
    <string name="cancel">取消</string>
</resources>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/user"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.205"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.442" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/password"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.216"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editTextTextPersonName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.651"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.434" />

    <EditText
        android:id="@+id/editTextTextPersonName2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.651"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/login"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.343"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.594" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cancel"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.721"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.594" />
</androidx.constraintlayout.widget.ConstraintLayout>

二、颜色资源

  1. 颜色资源文件位于res/values目录下,根元素是< resources >< /resources >标记,在该元素中,使用< color >< /color >标记定义各颜色资源,其中,通过为< color >< /color >标记设置name属性来指定颜色资源的名称,在其实标记< color >和< /color >中间添加颜色值。

步骤

color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#6200EE</color>
    <color name="colorPrimaryDark">#3700B3</color>
    <color name="colorAccent">#03DAC5</color>
    <color name="textColor">#1f72cd</color>
    <color name="btnColor">#13227a</color>
</resources>

在activity_main.xml对应位置引用,如——

        android:textColor="@color/btnColor"

三、尺寸资源

  1. 颜色资源文件位于res/values目录下,根元素是< resources >< /resources >标记,在该元素中,使用< dimen >< /dimen >标记尺寸资源,其中,通过为< dimen >< /dimen >标记设置name属性来指定尺寸资源的名称,在其实标记< dimen >和< /dimen >中间添加定义一个尺寸常量。
  2. 尺寸资源是没有定义的,需要自己写一个文件。

步骤

在这里插入图片描述
在这里插入图片描述
dimen.xml——

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="textSize">20dp</dimen>
    <dimen name="btnSize0">33dp</dimen>
</resources>

在activity_main.xml对应位置引用,如——

android:textSize="@dimen/textSize"

四、数据资源

  1. 数组资源文件位于res/values目录下,根元素是< resources >< /resources >标记,在该元素中,包括以下三个子元素:
< arrary / >子元素用于定义普通类型的数组
< integer-array / > 子元素用于定义整数数组
< string-array / >子元素用于定义字符串数组
  1. 可以定义在strings.xml文件中
    在这里插入图片描述
    在这里插入图片描述
    引用——可以通过Spinner引用
    在Spinner种引用——
android:entries="@array/item"
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值