Android定制屬于你自己的導航欄

openkk 12年前發布 | 111K 次閱讀 Android Android開發 移動開發

在實際開發中,我們時常要用到上方的兩個按鈕,通俗的我們可以叫做導航,等等.還是先看今天需要要實現的一個最的效果:

Android定制屬于你自己的導航欄

其實實現這樣的效果有多種方式,今天我要給大家要介紹的就是如何的去定制自己的控件,也就是自定義控件,自定義控件分為多種,有組合控件,有重寫在原來已有的控件上做基礎的修改,也有自己重寫寫一個類繼承于View對象,這方面的知識在實際開發當中也會常碰到,當然像我們這種菜鳥在這方面也是最欠缺的一個知識點,我希望通過我的一些講解或者分享能幫助到大家吧。今天我給大家講的就是組合控件的自定義,在后續的博文中我也希望自己能發現更多的這種自定義控件的能力然后再與大家分享,同樣如果大家有好的分享的東西也可以與我一起分享。就拿今天的一個效果如何去實現自定義控件,我會采用兩種方式去實現,一種就是純粹的代碼方式實現,一種就是用LayoyutInfalter去實現。

先說XML方式的實現方式:

先定義一個XML布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="45dip"
    android:background="@drawable/navigation_bg"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btn_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10.0dip"
        android:background="@drawable/backbg"
        android:text="返回"
        android:textColor="@android:color/white" />

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:singleLine="true"
        android:text="標題"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btn_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10.0dip"
        android:background="@drawable/buttonbg"
        android:text="新增"
        android:textColor="@android:color/white" />

</LinearLayout>

然后我們再重新寫一個類繼承于LineaLayour。通過在構造方法中去加載這個布局文件,這樣就能實現一個組合控件的定義了,這樣的方式是不是特別簡單。

public class UINavigationView2 extends LinearLayout {

    private Button btn_left;
    private Button btn_right;
    private TextView tv_title;

    public UINavigationView2(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public UINavigationView2(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layoutInflater.inflate(R.layout.title_bar, null);
        btn_left = (Button) findViewById(R.id.btn_left);
        btn_right = (Button) findViewById(R.id.btn_right);
        tv_title = (Button) findViewById(R.id.tv_title);

    }

    public void setBtnLeftBacground(int resId) {

        if (btn_left != null) {
            btn_left.setBackgroundResource(resId);
        }
    }

    public void setBtnRightBacground(int resId) {
        if (btn_right != null) {
            btn_right.setBackgroundResource(resId);
        }

    }

    public void setTvTitle(int resId) {
        if (tv_title != null) {
            tv_title.setText(resId);
        }
    }

    public Button getBtn_left() {
        return btn_left;
    }

    public Button getBtn_right() {
        return btn_right;
    }

    public TextView getTv_title() {
        return tv_title;
    }

}

接下來我們再按如果通過代碼來定義控件,這里需要了解的一個知識點,如果我們需要在我們的自定義控件如果新添屬于我們自己的屬性呢?這里我就們就需要attr.xml文件中自己定義,關于這方面的知識大家可以參考這篇文章:http://huangbo-2020.iteye.com/blog/1477611

我們先看我們自己定義的控件定義新增的如下幾個屬性:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="navigation">
        <attr name="btn_leftText" format="string" />
        <attr name="btn_rightText" format="string" />
        <attr name="tv_title" format="string" />
        <attr name="left_drawable" format="reference"></attr>
        <attr name="right_drawable" format="reference"></attr>
    </declare-styleable>

</resources>

再看我們的自定義控件的代碼中如何寫的:

public class UINavigationView extends LinearLayout {

    private Button btn_left;
    private Button btn_right;
    private TextView tv_title;
    private String strBtnLeft;
    private String strBtnRight;
    private String strTitle;
    private int left_drawable;
    private int right_drawable;

    public UINavigationView(Context context) {
        super(context);
        initContent();
    }

    public UINavigationView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttributes(attrs);
        initContent();
    }

    private void initAttributes(AttributeSet attributeSet) {

        if (null != attributeSet) {

            final int attrIds[] = new int[] { R.attr.btn_leftText,
                    R.attr.btn_rightText, R.attr.tv_title,
                    R.attr.left_drawable, R.attr.right_drawable };

            Context context = getContext();

            TypedArray array = context.obtainStyledAttributes(attributeSet,
                    attrIds);

            CharSequence t1 = array.getText(0);
            CharSequence t2 = array.getText(1);
            CharSequence t3 = array.getText(2);
            left_drawable = array.getResourceId(3, 0);
            right_drawable = array.getResourceId(4, 0);

            array.recycle();

            if (null != t1) {
                strBtnLeft = t1.toString();
            }
            if (null != t2) {
                strBtnRight = t2.toString();

            }
            if (null != t3) {
                strTitle = t3.toString();
            }

            Log.i("coder", "t1 = " + t1);
            Log.i("coder", "t2 = " + t2);
            Log.i("coder", "t3 = " + t3);
            Log.i("coder", "left_res = " + left_drawable);
        }

    }

    private void initContent() {

        Log.i("coder", "-----initContent----");
        // 設置水平方向
        setOrientation(HORIZONTAL);

        setGravity(Gravity.CENTER_VERTICAL);
        // 設置背景
        setBackgroundResource(R.drawable.navigation_bg);

        Context context = getContext();

        btn_left = new Button(context);
        btn_left.setVisibility(View.INVISIBLE);// 設置設置不可見
        if (left_drawable != 0) {
            btn_left.setBackgroundResource(left_drawable);
        } else {

            btn_left.setBackgroundResource(R.drawable.backbg);// 設置背景
        }
        btn_left.setTextColor(Color.WHITE);// 字體顏色

        if (null != strBtnLeft) {
            LayoutParams btnLeftParams = new LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            btnLeftParams.setMargins(10, 0, 0, 0);
            btn_left.setLayoutParams(btnLeftParams);
            btn_left.setText(strBtnLeft);
            btn_left.setVisibility(View.VISIBLE);
        } else {
            btn_left.setLayoutParams(new LayoutParams(50, 50));
        }

        // 添加這個按鈕
        addView(btn_left);

        //
        tv_title = new TextView(context);

        LayoutParams centerParam = new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT);
        centerParam.weight = 1;
        tv_title.setLayoutParams(centerParam);
        tv_title.setTextColor(Color.WHITE);

        if (null != strTitle) {
            tv_title.setText(strTitle);
        }

        tv_title.setGravity(Gravity.CENTER);
        btn_left.setVisibility(View.VISIBLE);
        // 添加這個標題
        addView(tv_title);

        btn_right = new Button(context);
        btn_right.setVisibility(View.INVISIBLE);// 設置設置不可見
        btn_right.setBackgroundResource(R.drawable.buttonbg);// 設置背景
        btn_right.setTextColor(Color.WHITE);// 字體顏色

        if (right_drawable != 0) {
            btn_right.setBackgroundResource(right_drawable);
        } else {

            btn_right.setBackgroundResource(R.drawable.backbg);// 設置背景
        }
        if (null != strBtnRight) {

            LayoutParams btnRightParams = new LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            btnRightParams.setMargins(0, 0, 10, 0);
            btn_right.setLayoutParams(btnRightParams);

            btn_right.setText(strBtnRight);
            btn_right.setVisibility(View.VISIBLE);
        } else {
            btn_right.setLayoutParams(new LayoutParams(50, 50));
        }

        // 添加這個按鈕
        addView(btn_right);

    }

    public Button getBtn_left() {
        return btn_left;
    }

    public Button getBtn_right() {
        return btn_right;
    }

    public TextView getTv_title() {
        return tv_title;
    }

    public String getStrBtnLeft() {
        return strBtnLeft;
    }

    public void setStrBtnLeft(String strBtnLeft) {
        this.strBtnLeft = strBtnLeft;
    }

    public String getStrBtnRight() {
        return strBtnRight;
    }

    public void setStrBtnRight(String strBtnRight) {
        this.strBtnRight = strBtnRight;
    }

    public String getStrTitle() {
        return strTitle;
    }

    public void setStrTitle(String strTitle) {
        this.strTitle = strTitle;
    }

    public int getLeft_drawable() {
        return left_drawable;
    }

    public void setLeft_drawable(int left_drawable) {
        this.left_drawable = left_drawable;
    }

    public int getRight_drawable() {
        return right_drawable;
    }

    public void setRight_drawable(int right_drawable) {
        this.right_drawable = right_drawable;
    }

}

如何去引用或者使用我們的那個新添加的屬性呢?首先要引入那個命名空間[我這是引用C#使用自定義控件的說法,暫時也通俗的這樣理解吧]:

xmlns:navigation="http://schemas.android.com/apk/res/com.jiahui.titlebar"

然后再使用我們自定義的屬性: navigation:btn_rightText,   navigation:btn_leftText等等 .

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:navigation="http://schemas.android.com/apk/res/com.jiahui.titlebar"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <com.jiahui.titlebar.UINavigationView
        android:id="@+id/uinavigationView"
        android:layout_width="fill_parent"
        android:layout_height="45dip"
        navigation:btn_leftText="@string/back"
        navigation:btn_rightText="@string/done"
        navigation:left_drawable="@drawable/backbg"
        navigation:right_drawable="@drawable/buttonbg"
        navigation:tv_title="My Application" />

</LinearLayout>

好了,以上就是我要和大家分享的這點東西,有什么不懂的,可以看代碼里的注釋,代碼基本上全貼上了,還有需要代碼參考的可以

出處:http://blog.csdn.net/jiahui524

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!