Android的资源引用(3)(Layout、Menu、Style、Them、Attribute、assets)

一、Layout资源时放在/res/layout/目录下面,layout资源下面的根元素通常为布局管理器,在每个布局管理器中定义各种的view.

二、使用菜单menu资源,资源时放在/res/menu/目录下面,资源的根元素通常为<menu ...../>元素,其中menu的用法在,另一片有介绍menu的一些用法

三、样式Style,该资源存放在/res/values/目录下面,样式资源的根元素通常为<resources......./>,该元素中可以包含多个<style....../>子元素,每一个<style.../>定义一个样式。

<style...../>元素指定如下的两个属性。

name:样式的名称

parent:样式继承父样式,该样式继承父样式的全部定义,也可以覆盖父样式的指定格式

<style...../>包含多个<item..../>子元素。

如下:

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

    <style name="DefaultFontStyle">
        <item name="android:textSize">29sp</item>
        <item name="android:textColor">#101010</item>
    </style>

    <style name="DefaultBackgroud" parent="DefaultFontStyle">
        <item name="android:background">#3cf968</item>
    </style>
    <style name="DefaultStyle" parent="@style/DefaultBackgroud">
    </style>
    <style name="TextViewFontStyle">
        <item name="android:textSize">30sp</item>
    </style>

    <style name="ButtonDefault">
        <item name="android:background">@drawable/pzb</item>
        <item name="android:focusable">true</item>
        <item name="android:clickable">true</item>
        <item name="android:layout_margin">20dp</item>
    </style>

    <style name="EditTextDefault">
        <item name="android:background">@drawable/ps</item>
        <item name="android:textColor">#ec7272</item>
        <item name="android:layout_marginLeft">10dp</item>
        <item name="android:layout_marginRight">10dp</item>
    </style>

    <style name="ButtonChange">
        <item name="android:background">@drawable/button_selector</item>
        <item name="android:layout_margin">20dp</item>
    </style>

    <style name="Button_gradient">
        <item name="android:textSize">20sp</item>
        <item name="android:background">@drawable/button_selector_gradient</item>
    </style>
</resources>

在布局文件中可已使用

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.asus.styletheme.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="测试andoid的style和theme"
        android:id="@+id/test_style"
        style="@style/TextViewFontStyle"/>
    <Button
        android:layout_width="30dp"
        android:layout_height="20dp"
        android:id="@+id/pz"
        style="@style/ButtonDefault"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="37dp"
        android:id="@+id/edit_test"
        style="@style/EditTextDefault"/>
    <Button
        android:layout_width="50dp"
        android:layout_height="50dp"
        style="@style/ButtonChange"
        android:id="@+id/button_test"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button_gradient"
        android:text="test"
        style="@style/Button_gradient"/>
</LinearLayout>

效果图

四、主题资源使用,主题资源通常也放在/res/values/目录下面,主题资源的xml文件同样也是以<resource..../>作为根元素,同样也是使用<style..../>元素来定义主题样式。

主题和样式的区别:

1、主题不能作用于单个的view组件上面,主题应该是对整个应用中的所有activity起作用,或者对指定的activity起作用。

2、主题定义的格式应该是改变外观的格式,比如:窗口标题,窗口边框

如:

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

</resources>

使用:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <service android:name=".service.MyService"></service>
    <service android:name=".service.MyService1"></service>
    <service android:name=".service.ChangeService"></service>

    <receiver android:name=".SmsReceiver.SmsReceiver">
        <intent-filter><action android:name="android.provider.Telephony.SMS_RECEIVED"/>
        </intent-filter>
    </receiver>


    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

五、属性attribute资源的使用:资源也是存放在/res/values目录下面,根元素也是<resource..../>

包含两个属性:

1、attr子元素:定义一个属性

2、declare-styleable子元素,内一个styleable对象就是一组attr属性组合:

七、assets资源

       除了上面介绍的各种XML文件、图片文件之外,Android应用可能还需要用到大量其他类型的资源,比如声音资源等。实际上,声音对于Android应用非常重要,选择合适的音效可以让Android应用增色不少。类似于声音文件及其他各种类型的文件,只要Android没有为之提供专门的支持,这种资源都被称为原始资源。Android 的原始资源可以放在如下两个地方。
  1、位于/res/raw/目录下,Android SDK会处理该目录下的原始资源,Android SDK会在R清单类中为该目录下的资源生成一个索引项。
  2、位于/assets/目录下,该目录下的资源是更彻底的原始资源。Android 应用需要通过AssetManager来管理该目录下的原始资源。


  Android SDK会为位于/res/raw/目录下的资源在R清单类中生成一个索引项,接下来在XML文件中可通过如下语法格式来访问它:
  @[<package_ name>: ]raw/file_ name
  在Java代码中则按如下语法格式来访问:
  [<package_ name>.]R.raw. <file_ name>

通过上面的索引项,Androiad 应用就可以非常方便地访问/raw目录下的原始资源了。至于获取原始资源之后如何处理,则完全取决于实际项目的需要。AssetManager是一个 专门管理assets目录下原始资源的管理器类,AssetManager提供了如
下两个常用方法来访问Assets资源。
1、InputStream open(String fileName): 根据文件名来获取原始资源对应的输入流。
2、AssetFileDescriptor openFd(String fileName): 根据文件名来获取原始资源对应的AssetFileDescriptor。AssetFileDescriptor 代表了一项原始资源的描述,应用程序可通过AssetFileDescriptor来获取原始资源。

实例:

创建raw和assets文件,分别在其中添加两个.mp3的文件,实现音乐播放.

package com.example.asus.summary2;

import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.io.IOException;

/**
 * Created by asus on 2018/7/28.
 */

public class AssetsActivity extends AppCompatActivity implements View.OnClickListener{

    private MediaPlayer mediaPlayer1;
    private MediaPlayer mediaPlayer2;
    protected void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);
        setContentView(R.layout.ass_activity);
        Button raw_m=findViewById(R.id.raw_m);
        Button assets_m=findViewById(R.id.assets_m);
        raw_m.setOnClickListener(this);
        assets_m.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.assets_m:{
                AssetManager manager=getAssets();
                try {
                    AssetFileDescriptor descriptor=manager.openFd("ass.mp3");
                    mediaPlayer2=new MediaPlayer();
                    mediaPlayer2.setDataSource(descriptor.getFileDescriptor());
                    mediaPlayer2.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mediaPlayer2.start();
                break;
            }
            case R.id.raw_m:{
                mediaPlayer1=MediaPlayer.create(this,R.raw.ra);
                try {
                    mediaPlayer1.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mediaPlayer1.start();
                break;
            }
            default:
                break;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/wenge1477/article/details/81370088