Android peferenceActivity 自訂標題簡單方法,android自訂標題

來源:互聯網
上載者:User

Android peferenceActivity 自訂標題簡單方法,android自訂標題
Android peferenceActivity 自訂標題簡單方法

peferenceActivity 完全使用定義好的布局。
因此不能簡單象其它好視窗進行自定,現在我們需要加
一個自訂標題,比如象其它視窗一樣加一個統一topbar.
假設這個topbar的布局是 title.xml

一.標準自訂標題列方法

Android 提供自訂標題列方法
我們簡單實現。

@Override
protected void onCreate(Bundle savedInstanceState) {
final boolean isCustom =requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_setting);
addPreferencesFromResource(R.xml.setting_preference);

if(isCustom)
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);

但是在Android 4.0會提示

You cannot combine custom titles with other title features

網上提供很多複雜辦法解決,關鍵在PerferenceActivtiy 總是失敗。

二.自訂一個perfence 布局並作為第一個配置項。

在xml/perference.xml 布局檔案下加入一個

<Preference android:layout="@layout/title" android:key="app_info"/>

這樣在可以實現偽標題列,這種方法布局缺點是無法鋪滿整個視窗,即在布局兩側會出一個白邊,非常不好看。

三.建一個自訂視窗布局

在查詢資料發現,perferenceActivity是可以支援完全的自訂布局的,只要保證布局中一個listView,它的id是
android:id="@android:id/list" 即可

在調用addPreferencesFromResource(); 後,perference列表會自動加入到這個listView當中。而標題布局,簡單加個在listView之上即可。

這是成功的布局,效果非常令人滿意。

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@color/bg_color" >

<include android:id="@+id/title" layout="@layout/title"></include>
<ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:cacheColorHint="@color/transparent" android:scrollbarStyle="outsideOverlay" android:fadingEdgeLength="0dp" android:scrollbars="none" android:fadingEdge="none" android:listSelector="#00000000" />
</LinearLayout>

而且這個還會帶來額外的好處是,可以自訂perference list的背景色之類


android開發中怎把標題置中,不是自訂標題,也就是說不需要xml,可以實現?可以的話麻煩貼下方法

如果是在源碼環境下,修改預設的Title布局檔案(screen_title.xml等)是可以實現的。
非源碼環境下,實現不了。
 
android開發中怎自訂標題列

blog.csdn.net/...715678
最簡單的方式
 

聯繫我們

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