歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android定制屬於你自己的導航欄

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

日期:2017/3/1 9:58:03   编辑:Linux編程

在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>

Copyright © Linux教程網 All Rights Reserved