天天看點

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;
        }
    }
}
           

運作結果:

繼續閱讀