Android布局PreferenceActivity——插入自訂布局

來源:互聯網
上載者:User

本文地址:http://blog.csdn.net/you_and_me12/article/details/8655234


2013-03-10

導語:PreferenceActivity是一個方便設定管理的介面,但是對於介面顯示來說比較單調,所以自訂布局就很有必要了。本文舉例說明在Preference中自訂layout的方法。筆者是為了在設定中插入@有米v4廣告條才研究了一晚上的。


本文:首先PreferenceScreen是一個xml檔案於res/xml目錄下,不屬於layout檔案。要插入layout,有兩種方法。


1.使用Preference的android:@layout屬性

1)xml檔案中preference的添加

<Preference android:layout="@layout/youmi_ad" android:key="youmi_ad"/>

2)layout.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="fill_parent"  android:layout_height="wrap_content"  >  <!-- 有米廣告 -->    <akai.settings.YoumiAd         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:gravity="center_horizontal"        /></LinearLayout>

3)由於v4版本的廣告條需要自己定義layout載體,所以這裡重載了一個LinearLayout,自訂布局也是如此。

在YoumiAd.java中處理布局檔案,其他布局類似,不再敘述。

public class YoumiAd extends LinearLayout{public YoumiAd(Context context) {super(context);init(context);}public YoumiAd(Context context, AttributeSet attrs) {super(context, attrs);init(context);}private void init(Context context) {//init youmi adAdView adView = new AdView(context, AdSize.SIZE_320x50);this.addView(adView);adView.setAdListener(new AdViewLinstener() {@Overridepublic void onSwitchedAd(AdView arg0) {Log.i("Youmi", "廣告條切換");}@Overridepublic void onReceivedAd(AdView arg0) {Log.i("Youmi", "請求廣告成功");}@Overridepublic void onFailedToReceivedAd(AdView arg0) {Log.i("Youmi", "請求廣告失敗");}});}}

2.將layout添加到Activity中 setContentView(R.layout.youmi_ad)以自訂內容

1)xml檔案中preference的添加

<Preference android:layout="@layout/youmi_ad" android:key="youmi_ad"/>

2)layout.xml,需要添加一個ListView控制項,且id為list,不然不能運行,應該是由於PreferenceActivity是一個List的原因吧。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="fill_parent"  android:layout_height="wrap_content"  >  <ListView       android:id="@android:id/list"       android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:drawSelectorOnTop="false"       android:layout_weight="1"       />  <!-- 有米廣告 --><LinearLayout         android:id="@+id/adLayout"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:gravity="center_horizontal"        /></LinearLayout>

3)在Activity中的onCreate添加setContentView(R.layout.youmi_ad);然後就可以使用findViewById去擷取自訂布局下的控制項了。

結尾: 1)寫完了,睡覺去啦

      2)繼續學習Android

      3)我是IT程式猿


聯繫我們

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