第二十五篇-Android 应用资源

时间:2023-03-09 16:24:26
第二十五篇-Android 应用资源

这里介绍android的一些资源文件。

以一个登录界面为例。

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"> <TextView
android:textColor="@color/text_color"
android:textSize="@dimen/text_size"
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/user_name" /> <EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"> <TextView
android:textColor="@color/text_color"
android:textSize="@dimen/text_size"
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/password" /> <EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"> <Button
android:textColor="@color/button_color"
android:textSize="@dimen/button_size"
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login" /> <Button
android:textColor="@color/button_color"
android:textSize="@dimen/button_size"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancle" />
</LinearLayout>
</LinearLayout>

res/values下面有许多xml文件。colors.xml是存放颜色的文件。引用它可以@color/text_color,里面的颜色可以自己添加,name可以自己定义,最好可以看出你要引用的地方,方便阅读与修改。

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="text_color">#0000ff</color>
<color name="button_color">#00ff00</color>
</resources>

strings.xml里面保存的是一些字符串,引用方式@string/text_name,同样是通过name进行引用的,所以name要能有代表性

<resources>
<string name="app_name">ResouceTest</string>
<string name="user_name">用户名</string>
<string name="password">密码</string>
<string name="cancle">取消</string>
<string name="login">登录</string>
</resources>

资源文件里可能没有之存放的地方,我们可以自己建一个Values XML files,创建方法和layout XML创建方法相同。建一个values.xml,然后里面存放一些值,比如说text_size

<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">18dp</dimen>
<dimen name="button_size">23dp</dimen>
</resources>

将这些文件归类,便于维护和修改。