安卓Android打造屬於你自己的TitleBar自定義控件模版

在我們實現需求的時候,有時會遇到菜單欄的多次使用,內容不一,但是結構類似,所以我們就需要製作一個模版,以適配不同情況。大概的樣式就是這樣的:




下面我們就使用自定義控件實現一下這種需求。

1.先創建一個自定義控件,構造函數使用前三個就行

    public TitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleBar(Context context) {
        this(context, null);
    }

    public TitleBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs);
        this.mContext = context;

        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TitleViewBar, defStyle, 0);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.TitleViewBar_model:
                    model = a.getInt(attr, 0);//拿到xml註冊的屬性值
                    modelInt = model;
                    break;
            }
        }
        a.recycle();//必須有這句

爲能了能在頁面靜態設置自定義控件的自定義屬性,所以使用了TypedArray 來獲取xml裏面的自定義的屬性 

解釋一下:

attrs爲檢索的屬性容器(集合好聽些)

obtainStyledAttributes(attrs, R.styleable.TitleViewBar, defStyle, 0) :返回一個TitleViewBar控件的屬性集合

a.recycle() :返回先前檢索的數組,稍後再用,這語句必須要寫上,就相當於show()方法一樣

switch (attr) {
                case R.styleable.TitleViewBar_model:
                    model = a.getInt(attr, 0);//拿到xml註冊的屬性值
                    modelInt = model;
                    break;
            }
R.styleable.TitleViewBar_model


這是attrs資源文件設置的屬性名稱

並且定義了其屬性爲integer,我們獲取其值以進行判斷,給控件設置data數據等等

接下來是將我們寫好的佈局文件放入到控件中已備使用:


View view = LayoutInflater.from(context).inflate(R.layout.title_layout, null);
        tv_center = (TextView) view.findViewById(R.id.tv_center);
        img_back = (ImageView) view.findViewById(R.id.img_back);
        tv_loction = (TextView) view.findViewById(R.id.tv_loction);
        ft_right = (FrameLayout) view.findViewById(R.id.ft_right);

        RelativeLayout relativeLayout = new RelativeLayout(context);
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                DensityUtils.dp2px(mContext, 48));//dp轉化爲px
        relativeLayout.setLayoutParams(layoutParams);

        ft_right.setVisibility(VISIBLE);
        ft_right.setOnClickListener(this);
        img_back.setOnClickListener(this);

        relativeLayout.addView(view);
        addView(relativeLayout);

        view_pop = LayoutInflater.from(mContext).inflate(R.layout.layout_titlebar_lv, null);
        mListView = (ListView) view_pop.findViewById(R.id.lv_pop);
上面創建了兩個佈局文件 一個加入自定義控件(R.layout.title_layout),另一個加入到popupwindow中(R.layout.layout_titlebar_lv,),用以點擊右上角圖標彈出來

接下來別忘了設置數據setModel(model)

然後是popup和右上角圖片的點擊事件:

    public void onClick(View view) {
        if (view.getId() == R.id.ft_right) {
            adapter = new ListViewAdapter(mContext,mData);
            mListView.setAdapter(adapter);
            popupWindow = new PopupWindow(view_pop, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            // 設置SelectPicPopupWindow彈出窗體可點擊
            popupWindow.setFocusable(true);
            // 點列表外關閉列表
            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            popupWindow.showAsDropDown(tv_loction, DensityUtils.dp2px(mContext, -120), 0);
        } else {

        }
    }
然後是listviw的item的點擊事件:

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        mListView.setOnItemClickListener(onItemClickListener);
    }
然後設置好數據源:

    /**
     * 數據1
     * @return
     */
    private List<HashMap<String, Object>> getData1() {
        List<HashMap<String, Object>> mData = new ArrayList<>();
        HashMap<String, Object> map = new HashMap<>();
        map.put("icon", R.drawable.apply_angin);
        map.put("text", "再次申請");
        mData.add(map);
        map = new HashMap<>();
        map.put("icon", R.drawable.apply_spend);
        map.put("text", "結算詳情");
        mData.add(map);
        return mData;
    }

    /**
     * 數據2
     * @return
     */
    private List<HashMap<String, Object>> getData2() {
        List<HashMap<String, Object>> mData = new ArrayList<>();
        HashMap<String, Object> map = new HashMap<>();
        map.put("icon", R.drawable.apply_time);
        map.put("text", "本日費用");
        mData.add(map);
        map = new HashMap<>();
        map.put("icon", R.drawable.apply_time);
        map.put("text", "本月費用");
        mData.add(map);
        map = new HashMap<>();
        map.put("icon", R.drawable.apply_time);
        map.put("text", "本年費用");
        mData.add(map);
        return mData;
    }
這樣titlebar創建好了,然後是如何使用:

既然控件都創建好了,那麼使用起來應該是很簡單方便的,如果使用很複雜,那麼只能說明你的自定義控件還不達標。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_title_bar);

        titleBar = (TitleBar) this.findViewById(R.id.titleBar);
        button = (Button) this.findViewById(R.id.btn);
        button.setOnClickListener(this);

        titleBar.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                switch (i) {
                    case 0:
                        showToast("000000");
                        break;
                    case 1:
                        showToast("1111");
                        break;
                    case 2:
                        showToast("22222");
                        break;
                }
                titleBar.dismissPopWindow();

            }
        });
    }
相信大家都可以看到使用起來只需要調用剛剛我們創建好的接口,然後實現item的點擊接口就行了

可能說的不是很明白,大家可以看下代碼就知道了。

鏈接:http://download.csdn.net/download/qq_37173653/9968999


ps:下載csdn資源怎麼設置不需要積分呀,大佬說一下!






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章