Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

PreferenceActivity 自定義 Layout 的兩種做法

編輯:關於 Android 程式設計

PreferenceActivity 天生適合做設置界面,但外觀實在單調。想在其中插入自定義內容(筆者當時是為了塞一條 @有米 v4 廣告),就需要動點手腳。

首先要明確一點:PreferenceScreen 的配置文件放在 res/xml 目錄下,並不屬於 layout 資源。要往裡嵌布局,有兩條路。

方法一:使用 Preference 的 android:layout 屬性

1)在 xml 配置文件中添加 Preference 節點:

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

2)編寫 res/layout/youmi_ad.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://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,其他自定義布局也是同樣思路。

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) {
        AdView adView = new AdView(context, AdSize.SIZE_320x50);
        this.addView(adView);
        adView.setAdListener(new AdViewLinstener() {
            @Override
            public void onSwitchedAd(AdView arg0) {
                Log.i("Youmi", "廣告條切換");
            }
            @Override
            public void onReceivedAd(AdView arg0) {
                Log.i("Youmi", "請求廣告成功");
            }
            @Override
            public void onFailedToReceivedAd(AdView arg0) {
                Log.i("Youmi", "請求廣告失敗");
            }
        });
    }
}

方法二:用 setContentView 掛載整個布局

1)xml 中的 Preference 添加方式相同:

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

2)layout 中必須包含一個 id 為 @android:id/list 的 ListView,否則無法運行——畢竟 PreferenceActivity 本身就是基於 List 的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://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. 下一篇文章:
Copyright © Android教程網 All Rights Reserved