Java反射改变Android属性

Java反射改变Android属性

在某些情况下,Android体系里面的某些对象没有对外提供针对某个属性或者类,方法公开的get或者set方法,但是项目需要对这些需要修改和调整。就需要使用Java的反射机制修改Android的属性。
举一个简单的例子,通过Java反射机制改变Android ListView分割线的高度值。
Android的ListView本身有分割线,并对外提供了公开设置分割线高度的方法:
    /**
     * Sets the height of the divider that will be drawn between each item in the list. Calling
     * this will override the intrinsic height as set by {@link #setDivider(Drawable)}
     *
     * @param height The new height of the divider in pixels.
     */
    public void setDividerHeight(int height) {
        mDividerHeight = height;
        requestLayout();
        invalidate();
    }

该设置方法是设置了Android ListView内部定义的一个变量:
int mDividerHeight;
该变量最终决定了ListView的分割线高度值。
现在情况是Android官方提供了ListView的公开设置分割线高度值mDividerHeight的方法,但是有些情况下,Android的某些类或者一些第三方的代码没有提供像这样的公开设置的通道,那怎么办?就要靠Java反射机制了。
现在不使用ListView提供的puhlic setDividerHeight方法,而通过反射实现设置ListView的高度值mDividerHeight。反射机制首先要定位变量mDividerHeight,然后打开这个变量的访问属性,进而设置它的值。
package zhangphil.test;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.lang.reflect.Field;

public class ListDemoActivity extends android.app.ListActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LVAdapter adapter = new LVAdapter(this, 0);
        setListAdapter(adapter);

        try {
            //相当于:
            //this.getListView().setDividerHeight(80);
            setMy(this.getListView());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setMy(ListView lv) throws Exception {
        Class<?> listView = lv.getClass();
        Field mDividerHeight = listView.getDeclaredField("mDividerHeight");
        mDividerHeight.setAccessible(true);
        mDividerHeight.setInt(lv, 80);
    }

    private class LVAdapter extends ArrayAdapter {

        public LVAdapter(@NonNull Context context, int resource) {
            super(context, resource);
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getApplicationContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
            }

            TextView text = convertView.findViewById(android.R.id.text1);
            text.setText(position + "");
            text.setTextColor(getResources().getColor(android.R.color.holo_red_light));

            return convertView;
        }

        @Override
        public int getCount() {
            return 100;
        }
    }
}

运行结果:

猜你喜欢

转载自blog.csdn.net/zhangphil/article/details/80138465