安卓學習隨筆 -- 自訂標題列

來源:互聯網
上載者:User

標籤:android   自訂標題蘭   

在安卓中不喜歡系統預設的標題列,那麼如何讓自訂一個自己的標題列呢。

自訂後的標題列如下:



首先這裡需要定義一個自訂的標題列布局 title.xml檔案 (裡邊需要兩個圖片這個很簡單)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"         android:orientation="horizontal"      android:layout_width="fill_parent"       android:layout_height="50dp"       <!-- 新的 標題列的 高度-->    android:background="#f2f8f8">      <ImageButton        android:id="@+id/imageButton1"        android:layout_width="40dp"        android:layout_height="40dp"              android:layout_centerVertical="true"        android:layout_alignParentLeft="true"        android:layout_marginLeft="20dp"        android:src="@drawable/lbt" />            <TextView           android:layout_centerInParent="true"          android:layout_width="wrap_content"          android:layout_height="match_parent"          android:text="自訂標題列"          android:textSize="20sp"          android:gravity="center_vertical"          />    <ImageButton        android:id="@+id/imageButton1"        android:layout_width="40dp"        android:layout_height="40dp"        android:layout_centerInParent="true"        android:layout_alignParentRight="true"        android:layout_marginRight="20dp"        android:src="@drawable/rbt" />    </RelativeLayout>  

然後再MainActivity中聲明使用自訂的標題列

public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//聲明使用自訂的標題列requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);setContentView(R.layout.activity_main);//使用自訂的標題列getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title); }}

接下來我們再來設定標題列的高度 以及 處理兩側沒有完全覆蓋原始標題列的bug,這時 我們需要在定義一個style檔案 MyStyle.xml

在自訂標題列中經常回遇到沒有填充完全的效果如:


 MyStyle.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android"><style name="titleBarStyle" parent="android:Theme">     <item name="android:windowTitleSize">50dp</item><!-- 原始標題列的高度 -->     <item name="android:padding">0dp</item><!-- 使新的標題列完全延伸到對齊到原始標題列的兩邊 --></style></resources>


然後呢 在 AndroidManifest.xml 中給activity 添加 一個theme屬性

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.mytitle"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"         android:theme="@style/AppTheme" >        <activity            android:name="com.example.mytitle.MainActivity"             android:label="@string/app_name"             android:theme="@style/titleBarStyle">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>


在一樣中 有兩處給標題列添加了高度設定,一個是原始的標題列高度,一個是自訂標題列的高度。一般設為兩個相等。不然或者自訂的標題列只佔用了原始標題列的上部分。或者只顯示了自訂標題列的上部分。
到這裡基本就修改完成了。
源碼:http://download.csdn.net/detail/liuhenghui5201/7644123

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.