Android 反射-换一种方式编程

转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/59109933

本文出自【赵彦军的博客】

上一次写了一篇文章 Java 反射 使用总结 , 今天算是对反射的补充,只不过把反射用到了Android层面上。

首先创建工具类 ResourceUtils

package com.app.fanse;

import android.content.Context;
import android.graphics.drawable.Drawable; public class ResourceUtils { public static int getIdByName(Context context, String className, String name) {
return context.getResources().getIdentifier(name, className, context.getPackageName());
} /**
* 获取布局文件的资源ID
* @param context
* @param name
* @return
*/
public static int getIdFromLayout(Context context, String name) {
return getIdByName(context, "layout", name);
} /**
* 从控件中获取资源的ID
* @param context
* @param name
* @return
*/
public static int getIdFromId(Context context, String name) {
return getIdByName(context, "id", name);
} /**
* 从 strings.xml 里面获取资源的ID
* @param context
* @param name
* @return
*/
public static int getIdFromString(Context context, String name) {
return getIdByName(context, "string", name);
} /**
* 从 Drawable 里面获取资源的ID
* @param context
* @param name
* @return
*/
public static int getIdFromDrawable(Context context, String name) {
return getIdByName(context, "drawable", name);
} /**
* 从 Mipmap 里面获取资源的ID
* @param context
* @param name
* @return
*/
public static int getIdFromMipmap(Context context, String name) {
return getIdByName(context, "mipmap", name);
} /**
* 从 strings.xml 里面获取字符串
* @param context
* @param name
* @return
*/
public static String getResString(Context context, String name) {
return context.getString(getIdFromString(context, name));
} /**
* 从Drawable目录获取 Drawable 对象
* @param context
* @param name
* @return
*/
public static Drawable getDrawableFromString(Context context, String name ){
return context.getResources().getDrawable( getIdFromDrawable( context , name ) ) ;
} /**
* 从Mipmap目录获取 Drawable 对象
* @param context
* @param name
* @return
*/
public static Drawable getMipmapFromString(Context context, String name ){
return context.getResources().getDrawable( getIdFromMipmap( context , name ) ) ;
} }

工具类的使用

首先新建布局文件 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
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"
tools:context="com.app.fanse.MainActivity"> <TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" /> <ImageView
android:id="@+id/image"
android:layout_below="@id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>

然后创建MainActivity

package com.app.fanse;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView textView ;
private ImageView imageView ; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //获取布局文件id
int layoutID = ResourceUtils.getIdFromLayout( this , "activity_main" ) ;
setContentView( layoutID ); //获取TextView 的 id
textView = (TextView) findViewById( ResourceUtils.getIdFromId( this , "tv")); //获取 strings.xml 中的字符串
textView.setText( ResourceUtils.getResString( this , "tv_des")); //获取ImageView 的 id
imageView = (ImageView) findViewById( ResourceUtils.getIdFromId( this , "image")); //获取 Mipmap 里面的 Drawable
imageView.setImageDrawable( ResourceUtils.getMipmapFromString( this , "ic_launcher")); //获取 Drawable 里面的 Drawable
imageView.setImageDrawable( ResourceUtils.getDrawableFromString( this , "ic_launcher"));
}
}

那么效果怎么样呢?请看效果图


个人微信号:zhaoyanjun125 , 欢迎关注

Android 反射-换一种方式编程的更多相关文章

  1. Android 数据存储五种方式

    1.概述 Android提供了5种方式来让用户保存持久化应用程序数据.根据自己的需求来做选择,比如数据是否是应用程序私有的,是否能被其他程序访问,需要多少数据存储空间等,分别是: ① 使用Shared ...

  2. android 定位的四种方式

    [原文]  开发中对于地图及地理位置的定位是我们经常要用地,地图功能的使用使得我们应用功能更加完善,下面总结了一下网络中现有对于介绍android定位的4种方式,希望对大家有帮助: android 定 ...

  3. 【转】在Android Studio中下载Android SDK的两种方式(Android Studio3.0、windows)

    在Android Studio中下载Android SDK的两种方式(Android Studio3.0.windows) 方式一.设置HTTP Proxy1. 打开Settings2. 点击HTTP ...

  4. android 定位的几种方式介绍

    [地理位置] android 定位的几种方式介绍 开发中对于地图及地理位置的定位是我们经常要用地,地图功能的使用使得我们应用功能更加完善,下面 www.androidkaifa.com 总结了一下网络 ...

  5. Android学习—下载Android SDK的两种方式

    在Android Studio中下载Android SDK的两种方式 Android studio下载地址:http://www.android-studio.org/ 方式一.设置HTTP Prox ...

  6. 将Eclipse代码导入到Android Studio的两种方式

    转: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0104/2259.html 说到使用Android Studio,除了新建 ...

  7. Android数据存储五种方式总结

    本文介绍Android平台进行数据存储的五大方式,分别如下: 1 使用SharedPreferences存储数据     2 文件存储数据       3 SQLite数据库存储数据 4 使用Cont ...

  8. Android视频播放的两种方式介绍

    1.在Android 中播放视频的方式有两种: 第一种方式是使用MediaPlayer 结合SurfaceView 来播放,通过MediaPlayer来控制视频的播放.暂停.进度等: 通过Surfac ...

  9. Android事件处理的2种方式:监听器与回调

    android组件的事件处理有2种方式: 1.基于监听器的事件处理方式:先定义组件,然后为组件设定监听器. 详见http://blog.csdn.net/jediael_lu/article/deta ...

随机推荐

  1. wordpress数据表结构

    Table: wp_commentmeta Field Type Null Key Default Extra meta_id bigint(20) unsigned PRI NULL auto_in ...

  2. grunt live reload 配置记录

    1.npm install --save-dev grunt-contrib-watch  安装 watch 2.安装chrome livereload 插件 3.配置Gruntfile.js wat ...

  3. 会话Cookie及session的关系(Cookie & Session)

    会话Cookie及session的关系(Cookie & Session) 在通常的使用中,我们只知道session信息是存放在服务器端,而cookie是存放在客户端.但服务器如何使用sess ...

  4. 关于bootstrap的modal弹出层嵌套子Modal所引发的血案(转)

    原文地址 http://blog.csdn.net/liuxiaogangqq/article/details/51821359 bootstrap的弹出层嵌套有一个问题 ,当子modal关闭时父的m ...

  5. js原生设计模式——10适配器模式之参数适配器

    原理:参数适配器说白了就是给出要带入数据字段的对应字段的默认值,一旦数据字段值不足,就取默认值补足. [写法一]:直接返回 <!DOCTYPE html><html lang=&qu ...

  6. HDU-1862-EXCEL排序

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1862 这个题考的就是你对sort函数的掌握:会用sort函数对字符串,数字排序,只要懂得话题目很简单 ...

  7. 用c3m自动生成的化学机理文件导入mfix里需要注意的一些问题

    1. 首先是类似这种写法: Species_g(1) = "CH4" Species_Alias_g(1) = "CH4" 可能会报错,写在一行可能就好了,如: ...

  8. C# 程序只能执行一次

    应用程序的主入口点. //每一个程序只能运行一个实例 bool isRun = false; System.Threading.Mutex m = new System.Threading.Mutex ...

  9. PHP + Memcache 实现Session共享

    一.安装Memcache和PHP扩展 Windows下的Memcache安装:1. 下载memcache的windows稳定版,解压放某个盘下面,比如在c:\memcached2. 在终端(也即cmd ...

  10. SVGEditor

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...