Android定製屬於你自己的導覽列

來源:互聯網
上載者:User

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

其實實現這樣的效果有多種方式,今天我要給大家要介紹的就是如何的去定製自己的控制項,也就是自訂控制項,自訂控制項分為多種,有群組控制項,有重寫在原來已有的控制項上做基礎的修改,也有自己重寫寫一個類繼承於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>

好了,以上就是我要和大家分享的這點東西,有什麼不懂的,可以看代碼裡的注釋,代碼基本上全貼上了,還有需要代碼參考的可以留下方式.歡迎加入Android菜鳥聯盟:252773976

如需轉載引用請註明出處:http://blog.csdn.net/jiahui524

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.