自訂Android標題列TitleBar布局

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   io   os   ar   java   

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.main); //軟體activity的布局 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar); //titlebar為自己標題列的布局

Layout下建立titlebar.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="wrap_content" android:layout_gravity="center_vertical" android:orientation="horizontal" >    <Button android:id="@+id/back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/back" android:textSize="18sp" android:textColor="#FF0000FF" />    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/share" android:textSize="18sp" android:textColor="#FFFFFF00" /></LinearLayout>
這樣雖然可以在一定程度上定製標題列, 不過, 這裡無法改變標題列的高度和背景(背景設定之後會在兩端有兩個 非常難看的邊框). 據說, 原因是android 固有的. 這裡有修改方法:   原理是這樣的. 直接像上述代碼那樣添加title僅僅是把一個子介面添加到原有的title上的, 並沒有改變原來的屬性, 比如 標題列大小, 標題列背景. 這些需要在theme 主題裡面定義.   因此先定義一個style, 若修改背景請修改android:windowTitleBackgroundStyle   若修改標題列高度,請修改android:windowTitleSize
例子: <?xml version="1.0" encoding="utf-8"?><resources xmlns:android="http://schemas.android.com/apk/res/android"><style name="CustomWindowTitleBackground">       <item name="android:background">#565656</item></style><style name="test" parent="android:Theme">     <item name="android:windowTitleSize">50dp</item>     <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item></style></resources>
在程式的android manifest.xml中對應:   activity中添加屬性 android:theme = "@style/test" 就可以了
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.guardian" android:versionCode="1" android:versionName="1.0">    <application android:icon="@drawable/icon" android:label="@string/app_name" >        <activity android:name=".Guardian" android:label="@string/app_name" android:theme = "@style/test" //就在這裡 >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>    <uses-sdk android:minSdkVersion="4" /></manifest>
之後藉助於設定自訂的標題列xml檔案,就可以自訂標題列布局了

原文來自:http://www.blogjava.net/Green-nut/articles/332617.html

自訂Android標題列TitleBar布局

聯繫我們

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