解决方案 »

  1.   

    这是利用Java获取resource中的图片:package com.hades.gallery;import java.lang.reflect.Field;import java.util.ArrayList;import android.app.Activity;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageView; public class MainGalleryActivity extends Activity implements OnItemClickListener {   /**    * @蓝色着衣    * 使用反射机制来获取drawable里面的图片    */   private Gallery gallery;    @Override   protected void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.gallery);       gallery = (Gallery) findViewById(R.id.gallery);      gallery.setAdapter(new ImageAdapter(this));      // 设置gallery监听      gallery.setOnItemClickListener(this);   }    public void onItemClick(AdapterView<?> parent, View view, int position,   long id) {    MainGalleryActivity.this.setTitle(String.valueOf(position));   }   public class ImageAdapter extends BaseAdapter {      private Context mContext;      private ArrayList<Integer> imgList = new ArrayList<Integer>();      private ArrayList<Object> imgSize = new ArrayList<Object>();      public ImageAdapter(Context context) {        mContext = context;        /*         * 反射机制         */        // 得到drawable下所有图片的名字        Field[] fields = R.drawable.class.getDeclaredFields();        for (Field field : fields) {           if (!"ic_launcher".equals(field.getName())) {              try {                 int index = field.getInt(R.drawable.class);                 imgList.add(index);                  int size[] = new int[2];                 Bitmap bitmap = BitmapFactory.decodeResource(                      getResources(), index);                 size[0] = bitmap.getWidth();                 size[1] = bitmap.getHeight();                 imgSize.add(size);              } catch (IllegalArgumentException e) {                 e.printStackTrace();              } catch (IllegalAccessException e) {                 e.printStackTrace();              }           }        }      }       public int getCount() {        return imgList.size();      }       public Object getItem(int position) {        return position;      }       public long getItemId(int position) {        return position;      }       public View getView(int position, View convertView, ViewGroup parent) {        ImageView imgView = new ImageView(mContext);    imgView.setImageResource(imgList.get(position).intValue());        imgView.setScaleType(ImageView.ScaleType.FIT_XY);        int size[] = new int[2];        size = (int[]) imgSize.get(position);        imgView.setLayoutParams(new Gallery.LayoutParams(size[0], size[1]));        return imgView;      }   }}下面是gallery.xml
    <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center_horizontal"    android:orientation="vertical" >    <Gallery        android:id="@+id/gallery"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:spacing="10dp" /></RelativeLayout>
      

  2.   

     利用c#的反射机制得到类的信息: 
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Reflection;namespace GuiReflect
    {
        /// <summary>
        /// Form1 的摘要说明。
        /// </summary>
        public class Form1 : System.Windows.Forms.Form
        {
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.TextBox txtB_Name;
            private System.Windows.Forms.GroupBox groupBox1;
            private System.Windows.Forms.TextBox txtB_Method;
            private System.Windows.Forms.TextBox txtB_property;
            private System.Windows.Forms.TextBox txtB_parent;
            private System.Windows.Forms.Label label2;
            private System.Windows.Forms.Label label3;
            private System.Windows.Forms.Label label4;
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.Label label5;
            private System.Windows.Forms.Label labNameSpace;
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.Container components = null;        public Form1()
            {
                //
                // Windows 窗体设计器支持所必需的
                //
                InitializeComponent();
                //
                // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
                //
            }
            /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                if( disposing )
                {
                    if (components != null) 
                    {
                        components.Dispose();
                    }
                }
                base.Dispose( disposing );
            }        #region Windows 窗体设计器生成的代码
            /// <summary>
            /// 设计器支持所需的方法 - 不要使用代码编辑器修改
            /// 此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
                this.txtB_Name = new System.Windows.Forms.TextBox();
                this.label1 = new System.Windows.Forms.Label();
                this.groupBox1 = new System.Windows.Forms.GroupBox();
                this.labNameSpace = new System.Windows.Forms.Label();
                this.label5 = new System.Windows.Forms.Label();
                this.label4 = new System.Windows.Forms.Label();
                this.label3 = new System.Windows.Forms.Label();
                this.label2 = new System.Windows.Forms.Label();
                this.txtB_parent = new System.Windows.Forms.TextBox();
                this.txtB_property = new System.Windows.Forms.TextBox();
                this.txtB_Method = new System.Windows.Forms.TextBox();
                this.button1 = new System.Windows.Forms.Button();
                this.groupBox1.SuspendLayout();
                this.SuspendLayout();
                // 
                // txtB_Name
                // 
                this.txtB_Name.Location = new System.Drawing.Point(144, 30);
                this.txtB_Name.Name = "txtB_Name";
                this.txtB_Name.Size = new System.Drawing.Size(272, 21);
                this.txtB_Name.TabIndex = 0;
                this.txtB_Name.Text = "";
                // 
                // label1
                // 
                this.label1.Location = new System.Drawing.Point(40, 32);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(56, 16);
                this.label1.TabIndex = 1;
                this.label1.Text = "c#类名";
                this.label1.Click += new System.EventHandler(this.label1_Click);
                // 
                // groupBox1
                // 
                this.groupBox1.Controls.Add(this.labNameSpace);
                this.groupBox1.Controls.Add(this.label5);
                this.groupBox1.Controls.Add(this.label4);
                this.groupBox1.Controls.Add(this.label3);
                this.groupBox1.Controls.Add(this.label2);
                this.groupBox1.Controls.Add(this.txtB_parent);
                this.groupBox1.Controls.Add(this.txtB_property);
                this.groupBox1.Controls.Add(this.txtB_Method);
                this.groupBox1.Location = new System.Drawing.Point(16, 96);
                this.groupBox1.Name = "groupBox1";
                this.groupBox1.Size = new System.Drawing.Size(624, 360);
                this.groupBox1.TabIndex = 2;
                this.groupBox1.TabStop = false;
                this.groupBox1.Text = "结果显示";
                // 
                // labNameSpace
                // 
                this.labNameSpace.Location = new System.Drawing.Point(72, 24);
                this.labNameSpace.Name = "labNameSpace";
                this.labNameSpace.Size = new System.Drawing.Size(272, 23);
                this.labNameSpace.TabIndex = 7;
                this.labNameSpace.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
                // 
                // label5
                // 
                this.label5.Location = new System.Drawing.Point(16, 24);
                this.label5.Name = "label5";
                this.label5.Size = new System.Drawing.Size(56, 23);
                this.label5.TabIndex = 6;
                this.label5.Text = "命名空间";
                this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                // 
                // label4
                // 
                this.label4.Location = new System.Drawing.Point(462, 64);
                this.label4.Name = "label4";
                this.label4.TabIndex = 5;
                this.label4.Text = "父类列表";
                this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                // 
                // label3
                // 
                this.label3.Location = new System.Drawing.Point(264, 64);
                this.label3.Name = "label3";
                this.label3.TabIndex = 4;
                this.label3.Text = "属性列表";
                this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                // 
                // label2
                // 
                this.label2.Location = new System.Drawing.Point(24, 64);
                this.label2.Name = "label2";
                this.label2.Size = new System.Drawing.Size(176, 23);
                this.label2.TabIndex = 3;
                this.label2.Text = "方法列表";
                this.label2.TextAlign = System.Drawing.ContentAlignment.TopCenter;
                // 
                // txtB_parent
                // 
                this.txtB_parent.Location = new System.Drawing.Point(416, 96);
                this.txtB_parent.Multiline = true;
                this.txtB_parent.Name = "txtB_parent";
                this.txtB_parent.ScrollBars = System.Windows.Forms.ScrollBars.Both;
                this.txtB_parent.Size = new System.Drawing.Size(192, 240);
                this.txtB_parent.TabIndex = 2;
                this.txtB_parent.Text = "";
                // 
                // txtB_property
                // 
                this.txtB_property.Location = new System.Drawing.Point(226, 96);
                this.txtB_property.Multiline = true;
                this.txtB_property.Name = "txtB_property";
                this.txtB_property.ScrollBars = System.Windows.Forms.ScrollBars.Both;
                this.txtB_property.Size = new System.Drawing.Size(176, 240);
                this.txtB_property.TabIndex = 1;
                this.txtB_property.Text = "";
                // 
                // txtB_Method
                // 
                this.txtB_Method.Location = new System.Drawing.Point(24, 96);
                this.txtB_Method.Multiline = true;
                this.txtB_Method.Name = "txtB_Method";
                this.txtB_Method.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
                this.txtB_Method.Size = new System.Drawing.Size(176, 240);
                this.txtB_Method.TabIndex = 0;
                this.txtB_Method.Text = "";
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(520, 29);
                this.button1.Name = "button1";
                this.button1.TabIndex = 3;
                this.button1.Text = "获得列表";
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
               
      

  3.   

    接上: // Form1
                // 
                this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
                this.ClientSize = new System.Drawing.Size(656, 469);
                this.Controls.Add(this.button1);
                this.Controls.Add(this.groupBox1);
                this.Controls.Add(this.label1);
                this.Controls.Add(this.txtB_Name);
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
                this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
                this.MaximizeBox = false;
                this.Name = "Form1";
                this.Text = "代码反射机制的应用";
                this.groupBox1.ResumeLayout(false);
                this.ResumeLayout(false);        }
            #endregion        /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main() 
            {
                Application.Run(new Form1());
            }        private void label1_Click(object sender, System.EventArgs e)
            {
            
            }        private void button1_Click(object sender, System.EventArgs e)
            {
                labNameSpace.Text="";
                txtB_Method.Text="";
                txtB_property.Text="";
                txtB_parent.Text="";
                if(txtB_Name.Text=="")
                    txtB_Method.Text="类名不能为空";
                else
                {    
                    string test="System."+txtB_Name.Text.ToString().Trim();
                    try
                    {
                        Type carType=Type.GetType(test);
                        labNameSpace.Text=carType.Namespace.ToString();
                        //得到类的方法代码
                        MethodInfo [] methods=carType.GetMethods();                
                        int NumMethod=0;
                        foreach(MethodInfo method in methods)
                        {
                            NumMethod++;
                            txtB_Method.Text+=NumMethod+"->"+method.Name.ToString()+" ";                    
                        }
                        //得到类的属性的代码
                        PropertyInfo []  properties=carType.GetProperties();
                        int NumPropertie=0;
                        foreach(PropertyInfo propertie in properties)
                        {
                            NumPropertie++;
                            txtB_property.Text+=NumPropertie+"->"+propertie.Name.ToString()+" ";    
                        }
                        //得到类的层次机构的代码
                        int NumParent=0;
                        Type parent=carType.BaseType;                
                        while(parent!=null)
                        {
                            NumParent++;
                            txtB_parent.Text+=NumParent+"->"+parent.Name.ToString()+" ";
                            parent=parent.BaseType;
                        }
                    }
                    catch(TypeLoadException e1)
                    {
                        txtB_Method.Text=e1.Message;
                        
                    }
                    catch(Exception e2)
                    {
                        txtB_Method.Text=e2.Message;
                        
                    }
                    
                    
                }
            }
        }
    }
      

  4.   

        还是谢谢你这么长的回答 我看了下代码 我好像找到了怎么获取Drawable类的 方法
      Type.GetType(Resource.Drawable)  是不是就获得了 Drawable 的Type对象?  但是好像还是报了 错
      

  5.   

    Type.GetType(“Resource.Drawable”)  这样可以了 谢谢你