---
title: "有效的减少应用占用的空间——使用系统资源"
author: "jmeditor"
date: "2013-08-15T13:56:30Z"
tags: []
categories: ["移动开发"]
slug: "untitled"
draft: false
lastmod: "2013-08-15T13:56:59Z"
id: 1924940
url: "https://jmeditor.iteye.com/blog/1924940"
---

做安卓开发的，是不是总想把自己的应用做的足够强大，但空间足够小？可以需求方又要求很炫的效果，怎么办？

其实系统自带了很多风格的边框、图标等，而且不同版本之间略有差异，但功能是一样的。我们为什么放着这些现成的资源不用呢？下面就来看看系统都有什么吧。运行下面的例子，可以看到系统中所有drawable资源。

 

MainActivity.java

```
package com.jmeditor.sysresbrowser;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

	//浏览资源的序号
	private static int index = 0;
	//所有资源列表
	private static List<Map<String,Object>> rs;
	//显示图片的View
	private ImageView imageView;
	//显示图片的背景，因为有些只适合在黑背景下显示
	private LinearLayout imageBg;
	//显示数量
	private TextView textView;
	//显示当前图片的ID，方便引用
	private TextView tips;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		textView = (TextView)findViewById(R.id.textView);
		tips = (TextView)findViewById(R.id.tips);
		imageView = (ImageView)findViewById(R.id.imageView);
		imageBg = (LinearLayout)findViewById(R.id.imageBg);
		Button imageViewBtnPre = (Button)findViewById(R.id.imageViewBtnPre);
		Button imageViewBtnNext = (Button)findViewById(R.id.imageViewBtnNext);
		
		//读取系统所有的drawable资源
		try {
			Class c = Class.forName("android.R$drawable");
			Field[] fs = c.getDeclaredFields();
			rs = new ArrayList<Map<String,Object>>();
				
			for (Field field : fs) {
				if (field.getType().getName().equals("int") ) {
					Map<String,Object> m = new HashMap<String,Object>();
					m.put("name", field.getName());
					m.put("value", field.get(this));
					rs.add(m);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//上一张
		imageViewBtnPre.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				if(index > 0){
					index --;
					showImg();
				}
			}

			
		});
		//下一张
		imageViewBtnNext.setOnClickListener(new OnClickListener(){
			
			@Override
			public void onClick(View v) {
				if(index < rs.size() - 1){
					index ++;
					showImg();
				}
			}
			
		});
		
		//背景色
		this.findViewById(R.id.bg_1).setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				imageBg.setBackgroundColor(getResources().getColor(R.color.white));
			}
			
		});
		this.findViewById(R.id.bg_2).setOnClickListener(new OnClickListener(){
			
			@Override
			public void onClick(View v) {
				imageBg.setBackgroundColor(getResources().getColor(R.color.black));
			}
			
		});
		this.findViewById(R.id.bg_3).setOnClickListener(new OnClickListener(){
			
			@Override
			public void onClick(View v) {
				imageBg.setBackgroundColor(getResources().getColor(R.color.blue));
			}
			
		});
	}
	
	private void showImg() {
		imageView.setBackgroundResource(Integer.valueOf(rs.get(index).get("value").toString()));
		String s = getResources().getString(R.string.total_msg);
		textView.setText(s.replaceAll("\\{1\\}", "" + rs.size()).replaceAll("\\{2\\}", "" + (index+1)));
		tips.setText(rs.get(index).get("name").toString());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
```

 

布局文件activity\_main.xml

```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    
    <LinearLayout 
        android:id="@+id/imageBg"
        android:layout_width="fill_parent"
        android:layout_height="100dip"
        android:padding="5dip">
	    <ImageView 
	        android:id="@+id/imageView"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip">
		<TextView
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="@string/tips"/>
		<TextView
		    android:id="@+id/tips"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"/>
	</LinearLayout>
	<LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
	    <Button 
	        android:id="@+id/imageViewBtnPre"
	        android:text="@string/pre_img"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"/>
	    
	    <Button 
	        android:id="@+id/imageViewBtnNext"
	        android:text="@string/next_img"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"/>
	</LinearLayout>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/background_title"/>
	    
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
	    <View
	        android:id="@+id/bg_1"
	        android:layout_width="50dip"
	        android:layout_height="50dip"
	        android:layout_margin="5dip"
	        android:background="@color/white"/>
	    
	    <View
	        android:id="@+id/bg_2"
	        android:layout_width="50dip"
	        android:layout_height="50dip"
	        android:layout_margin="5dip"
	        android:background="@color/black"/>
	    
	    <View
	        android:id="@+id/bg_3"
	        android:layout_width="50dip"
	        android:layout_height="50dip"
	        android:layout_margin="5dip"
	        android:background="@color/blue"/>
    </LinearLayout>
</LinearLayout>
```

 

   
![](http://dl2.iteye.com/upload/attachment/0088/2251/14e5113c-5668-363e-b958-feca8a5fbc3d.png)