Android布局——Preference自定義layout的方法
更新時間:2013年06月04日 17:24:39 作者:
PreferenceActivity是一個方便設(shè)置管理的界面,但是對于界面顯示來說比較單調(diào),所以自定義布局就很有必要了,下面與大家分享下Preference中自定義layout的方法
導(dǎo)語:PreferenceActivity是一個方便設(shè)置管理的界面,但是對于界面顯示來說比較單調(diào),所以自定義布局就很有必要了。本文舉例說明在Preference中自定義layout的方法。筆者是為了在設(shè)置中插入@有米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 ad
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", "請求廣告失敗");
}
});
}
}
2.將layout添加到Activity中 setContentView(R.layout.youmi_ad)以自定義內(nèi)容
1)xml文件中preference的添加
<Preference android:layout="@layout/youmi_ad" android:key="youmi_ad"/>
2)layout.xml,需要添加一個ListView控件,且id為list,不然不能運行,應(yīng)該是由于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去獲取自定義布局下的控件了。
正文:首先PreferenceScreen是一個xml文件于res/xml目錄下,不屬于layout文件。要插入layout,有兩種方法。
1.使用Preference的android:@layout屬性
1)xml文件中preference的添加
復(fù)制代碼 代碼如下:
<Preference android:layout="@layout/youmi_ad" android:key="youmi_ad"/>
2)layout.xml
復(fù)制代碼 代碼如下:
<?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中處理布局文件,其他布局類似,不再敘述。
復(fù)制代碼 代碼如下:
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 ad
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", "請求廣告失敗");
}
});
}
}
2.將layout添加到Activity中 setContentView(R.layout.youmi_ad)以自定義內(nèi)容
1)xml文件中preference的添加
復(fù)制代碼 代碼如下:
<Preference android:layout="@layout/youmi_ad" android:key="youmi_ad"/>
2)layout.xml,需要添加一個ListView控件,且id為list,不然不能運行,應(yīng)該是由于PreferenceActivity是一個List的原因吧。
復(fù)制代碼 代碼如下:
<?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去獲取自定義布局下的控件了。
您可能感興趣的文章:
- Android自定義View實現(xiàn)垂直時間軸布局
- android layout 按比例布局的代碼
- Android中RecyclerView布局代替GridView實現(xiàn)類似支付寶的界面
- android動態(tài)加載布局文件示例
- android Activity相對布局的使用方法
- Android實現(xiàn)輸入法彈出時把布局頂上去和登錄按鈕頂上去的解決方法
- Android TabLayout(選項卡布局)簡單用法實例分析
- Android應(yīng)用借助LinearLayout實現(xiàn)垂直水平居中布局
- android LinearLayout和RelativeLayout組合實現(xiàn)精確布局方法介紹
- Android實現(xiàn)Z軸布局效果
相關(guān)文章
TextView使用SpannableString設(shè)置復(fù)合文本 SpannableString實現(xiàn)TextView的鏈接
這篇文章主要為大家詳細(xì)介紹了如何利用SpannableString實現(xiàn)TextView的鏈接效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08Android中AlertDialog各種對話框的用法實例詳解
這篇文章主要介紹了Android中AlertDialog各種對話框的用法在項目開發(fā)中經(jīng)常用的到,本文給大家介紹的非常詳細(xì),具有參考借鑒價值2016-04-04Android實現(xiàn)下拉刷新的視圖和圖標(biāo)的旋轉(zhuǎn)
本篇文章主要介紹了Android實現(xiàn)下拉刷新的視圖和圖標(biāo)的旋轉(zhuǎn)的實例,具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03Android Jetpack架構(gòu)組件 ViewModel詳解
這篇文章主要介紹了Android Jetpack架構(gòu)組件 ViewModel詳解,ViewModel類讓數(shù)據(jù)可在發(fā)生屏幕旋轉(zhuǎn)等配置更改后繼續(xù)存在,ViewModel類旨在以注重生命周期的方式存儲和管理界面相關(guān)的數(shù)據(jù)。感興趣可以來學(xué)習(xí)一下2020-07-07