Android 反射-换一种方式编程
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 反射-换一种方式编程的更多相关文章
- Android 数据存储五种方式
1.概述 Android提供了5种方式来让用户保存持久化应用程序数据.根据自己的需求来做选择,比如数据是否是应用程序私有的,是否能被其他程序访问,需要多少数据存储空间等,分别是: ① 使用Shared ...
- android 定位的四种方式
[原文] 开发中对于地图及地理位置的定位是我们经常要用地,地图功能的使用使得我们应用功能更加完善,下面总结了一下网络中现有对于介绍android定位的4种方式,希望对大家有帮助: android 定 ...
- 【转】在Android Studio中下载Android SDK的两种方式(Android Studio3.0、windows)
在Android Studio中下载Android SDK的两种方式(Android Studio3.0.windows) 方式一.设置HTTP Proxy1. 打开Settings2. 点击HTTP ...
- android 定位的几种方式介绍
[地理位置] android 定位的几种方式介绍 开发中对于地图及地理位置的定位是我们经常要用地,地图功能的使用使得我们应用功能更加完善,下面 www.androidkaifa.com 总结了一下网络 ...
- Android学习—下载Android SDK的两种方式
在Android Studio中下载Android SDK的两种方式 Android studio下载地址:http://www.android-studio.org/ 方式一.设置HTTP Prox ...
- 将Eclipse代码导入到Android Studio的两种方式
转: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0104/2259.html 说到使用Android Studio,除了新建 ...
- Android数据存储五种方式总结
本文介绍Android平台进行数据存储的五大方式,分别如下: 1 使用SharedPreferences存储数据 2 文件存储数据 3 SQLite数据库存储数据 4 使用Cont ...
- Android视频播放的两种方式介绍
1.在Android 中播放视频的方式有两种: 第一种方式是使用MediaPlayer 结合SurfaceView 来播放,通过MediaPlayer来控制视频的播放.暂停.进度等: 通过Surfac ...
- Android事件处理的2种方式:监听器与回调
android组件的事件处理有2种方式: 1.基于监听器的事件处理方式:先定义组件,然后为组件设定监听器. 详见http://blog.csdn.net/jediael_lu/article/deta ...
随机推荐
- Python3基础 pop() 删除 键为指定值的项
镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.-------------------------------------- ...
- sklearn数据预处理-scale
对数据按列属性进行scale处理后,每列的数据均值变成0,标准差变为1.可通过下面的例子加深理解: from sklearn import preprocessing import numpy as ...
- max-width
#main { max-width: 600px; margin: 0 auto; } <div id="main"> Using max-width instead ...
- JS判断doctype文档模式-document.compatMode
IE对盒模型的渲染在 Standards Mode和Quirks Mode是有很大差别的,在Standards Mode下对于盒模型的解释和其他的标准浏览器是一样,但在Quirks Mode模式下则有 ...
- codeforces 755C. PolandBall and Forest
C. PolandBall and Forest time limit per test 1 second memory limit per test 256 megabytes input stan ...
- 关于bootstrap的modal弹出层嵌套子Modal所引发的血案(转)
原文地址 http://blog.csdn.net/liuxiaogangqq/article/details/51821359 bootstrap的弹出层嵌套有一个问题 ,当子modal关闭时父的m ...
- HDU-4371-Alice and Bob
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4371 这题在比赛的时候看错了题意,卡了很久都没做出来,也没有再回头仔细去看题,所以没做出来,之后再看别 ...
- JavaSwing JScrollPane的使用
JavaSwing JScrollPane的使用: 参考:http://duyz.blog.ifeng.com/article/340649.html package com.srie.test; i ...
- Weblogic常见故障常:JDBC Connection Pools
http://blog.csdn.net/woshixuye/article/details/24122579 有些时候是数据库连接池出现了问题,测试的时候显示没有连接池了,重启WebLogic都不行 ...
- jQuery获取、设置title的值
获取值:var t = $(document).attr('title'); 设置值:$(document).attr('title','value');