res资源目录简介

简略介绍Android工程中的资源目录(resources),res。

Android里的资源指的是什么?

资源是指代码应用的附加文件和动态内容,例如位图、布局定义、界面字符串、动画阐明等。

把资源放进对应的目录后,可应用在我的项目 R 类中生成的资源 ID 来拜访这些资源。形如 R.drawable.icon,R.layout.main_activity。 R类是主动生成的。代表resources。

分组资源类型

将各类资源放入我的项目 res/ 目录的特定子目录中。 子目录的名字特地重要。咱们把不同的资源放到不同的子目录中。

  • animator/:用于定义属性动画的 XML 文件。
  • anim/:用于定义突变动画的 XML 文件。(属性动画也可保留在此目录中,但为了辨别这两种类型,属性动画首选 animator/ 目录。)
  • color/:用于定义色彩状态列表的 XML 文件。
  • drawable/:位图文件(.png、.9.png、.jpg、.gif)或编译为以下可绘制对象资源子类型的 XML 文件: 位图文件、九宫格(可调整大小的位图)、状态列表、形态、动画可绘制对象、其余可绘制对象。
  • mipmap/:实用于不同启动器图标密度的可绘制对象文件。利用图标放这里。
    mipmap前面跟着的dpi类别,比方hdpi mdpi,外面的图标尺寸大小是不同的。能够参考默认图标的大小,来切App的图标。如果要省事,能够用一个图标复制进各个目录中。
  • layout/: 用于定义用户界面布局的 XML 文件。放 layout 文件。
  • menu/: 用于定义利用菜单(如选项菜单、上下文菜单或子菜单)的 XML 文件。
  • raw/:需以原始模式保留的任意文件。如要应用原始 InputStream 关上这些资源,请应用资源 ID(即 R.raw.filename)调用 Resources.openRawResource()。 然而,如需拜访原始文件名和文件层次结构,则能够思考将某些资源保留在 assets/ 目录(而非 res/raw/)下。assets/ 中的文件没有资源 ID,因而只能应用 AssetManager 读取这些文件。
  • values/:蕴含字符串、整型数和色彩等简略值的 XML 文件。其余 res/ 子目录中的 XML 资源文件会依据 XML 文件名定义单个资源,而 values/ 目录中的文件可形容多个资源。对于此目录中的文件, <resources style="box-sizing: inherit; margin-top: 0px; margin-bottom: 0px;"> 元素的每个子元素均会定义一个资源。例如,<string style="box-sizing: inherit;"> 元素会创立 R.string 资源,<color style="box-sizing: inherit;"> 元素会创立 R.color 资源。因为每个资源均应用本人的 XML 元素进行定义,因而您能够随便命名文件,并在某个文件中放入不同的资源类型。
    然而,您可能须要将独特的资源类型放在不同的文件中,使其高深莫测。
    例如,对于可在此目录中创立的资源,上面给出了相应的文件名约定:
    arrays.xml:资源数组(类型数组)。
    colors.xml:色彩值。
    dimens.xml:尺寸值。
    strings.xml:字符串值。
    styles.xml:款式。
  • xml/:可在运行时通过调用 Resources.getXML() 读取的任意 XML 文件。各种 XML 配置文件(如可搜寻配置)都必须保留在此处。
  • font/:带有扩展名的字体文件(如 .ttf、.otf 或 .ttc),或蕴含 元素的 XML 文件。
留神:切勿将资源文件间接保留在 res/ 目录内,因为这样会造成编译谬误。

最开始阶段,咱们接触比拟多的是layout目录。如果要增加一些图片,能够间接放进drawable目录。

批改利用图标,应该放进mipmap对应的目录。例如mipmap-hdpi里的图标是72x72像素的,mipmap-mdpi是48x48像素的。 图省事的话,拿一个图标,别离复制进mipmap的所有dpi目录里,肯定要对立文件名,比方ic_your_launcher.png。 而后在清单文件AndroidManifest.xml中,批改icon。

<application        android:icon="@mipmap/ic_your_launcher"

前面如果咱们要定义Button,TextView的一些款式,比方设置色彩,背景。会接触到drawable目录。

shape的绘制和应用

工程目录中有一个drawable文件夹,外面寄存的是一些动态的图片资源文件。 比方位图文件(.png、.9.png、.jpg、.gif);或一些可绘制对象资源子类型的 XML 文件(本文称为drawable文件)。

当咱们想给button或者TextView设定背景时,咱们会想到纯色背景。如果要求圆角背景,或是渐变色背景,咱们该如何实现呢? 一种方法是制作相应的美术素材,也就是切图。另一种方法是应用xml格局的资源文件。 本文要介绍的是shape。应用这类资源,能够实现一些比较简单的美术设计。

例子

接下来咱们新建一个shape试试,要求带有外围边框,有圆角,外面用渐变色填充。 在res/drawable/目录中,右键新建一个Drawable Resource file文件,起名shape_rect_gradient_red.xml。 root element抉择shape

代码:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <stroke        android:width="2dp"        android:color="#0E30B1" />    <corners android:radius="6dp" />    <gradient        android:centerColor="#F55353"        android:endColor="#F71515"        android:startColor="#FD7575" /></shape>

as的左边能够关上预览,看看成果。 其中

  • android:shape="rectangle"示意的是抉择长方形的形态。
  • stroke标签代表的是边框。外面设定边框宽度是2dp,边框色彩是#0E30B1。
  • corners标签代表的是圆角。如果不设置,则默认为直角。这里咱们设定圆角的半径为6dp。
  • gradient示意渐变色。别离能够设置起始,两头和完结的色彩值。

在layout中,给Button的background设置应用这个shape。xml的文件名就是它的资源名称。

<Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@drawable/shape_rect_gradient_red" />

再略微欠缺一下这个Button。

<Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@drawable/shape_rect_gradient_red"        android:text="RFDev btn 1"        android:textAllCaps="false"        android:textColor="#ffffff" />

运行一下,能够看到成果。 

shape介绍

shape又称为“形态可绘制对象”。为了简便,以下都称作shape或者“shape文件”。 shape是一种在 XML 文件中定义的通用形态。通过编译后,能够失去GradientDrawable对象。

资源援用

  • 在 Java 中:R.drawable.filename
  • 在 XML 中:@[package:]drawable/filename

语法

下面那个栗子咱们意识了几个元素,gradient,corners和stroke。上面是语法的具体介绍。

<?xml version="1.0" encoding="utf-8"?><shape    xmlns:android="http://schemas.android.com/apk/res/android"    android:shape=["rectangle" | "oval" | "line" | "ring"] >    <corners        android:radius="integer"        android:topLeftRadius="integer"        android:topRightRadius="integer"        android:bottomLeftRadius="integer"        android:bottomRightRadius="integer" />    <gradient        android:angle="integer"        android:centerX="float"        android:centerY="float"        android:centerColor="integer"        android:endColor="color"        android:gradientRadius="integer"        android:startColor="color"        android:type=["linear" | "radial" | "sweep"]        android:useLevel=["true" | "false"] />    <padding        android:left="integer"        android:top="integer"        android:right="integer"        android:bottom="integer" />    <size        android:width="integer"        android:height="integer" />    <solid        android:color="color" />    <stroke        android:width="integer"        android:color="color"        android:dashWidth="integer"        android:dashGap="integer" /></shape>
圆角背景的例子

上文介绍了corners的用法。咱们来看一个圆角背景的实现办法。 新建文件shape_btn_2_normal.xml

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <solid android:color="#6CB3DD" />    <corners android:radius="25dp" /></shape>

在layout中给TextView应用这个背景。

<TextView        android:id="@+id/tv1"        android:layout_width="match_parent"        android:layout_height="50dp"        android:background="@drawable/shape_btn_2_normal"        android:gravity="center"        android:text="RFDev 圆角背景TextView 1"        android:textColor="#ffffff" />

TextView的高度设置成了50dp,而背景的圆角半径设置成了25dp。 运行能够看到成果。

如果想要渐变色,再减少gradient的设置就好。

代码中应用资源

在java代码中应用资源,比方在activity中设置背景。

Resources res = getResources();Drawable shape = ResourcesCompat.getDrawable(res, R.drawable.shape_btn_2_normal, getTheme());TextView tv = (TextView)findViewById(R.id.tv1);tv.setBackground(shape);

应用这种形式,咱们能够本人实现一些简略的按钮成果。更简单的色彩和成果,须要美术设计师的反对。

环形的例子

尺寸和长度本人设定。

环形 thumb_round_1.xml

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="oval">    <solid android:color="#ffffff" />    <stroke        android:width="@dimen/audio_seek_bar_thumb_ring_width"        android:color="#7095fc" />    <size        android:width="@dimen/audio_seek_bar_thumb_size"        android:height="@dimen/audio_seek_bar_thumb_size" /></shape>

带突变的环形 thumb_round_progress_1.xml

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">    <item>        <shape android:shape="oval">            <size                android:width="@dimen/audio_seek_bar_thumb_size"                android:height="@dimen/audio_seek_bar_thumb_size" />            <solid android:color="#ffffff" />        </shape>    </item>    <item>        <shape            android:innerRadius="4dp"            android:thicknessRatio="6"            android:shape="ring"            android:useLevel="false">            <gradient                android:endColor="#ffffff"                android:startColor="#7095fc"                android:type="sweep" />            <size                android:width="@dimen/audio_seek_bar_thumb_size"                android:height="@dimen/audio_seek_bar_thumb_size" />        </shape>    </item></layer-list>

Android零根底入门教程视频参考