Lead: PreferenceActivity is an interface for convenient settings and management, but it is monotonous for interface display, so it is necessary to customize the layout. This article provides examples to illustrate how to customize layout in Preference. I tried to insert @ youmi v4 ad strips in the settings for one night.
Body: PreferenceScreen is an xml file in the res/xml directory and does not belong to the layout file. There are two methods to insert layout.
1. Use the Preference android: @ layout attribute
1) Add preference in xml fileCopy codeThe Code is as follows: <Preference android: layout = "@ layout/youmi_ad" android: key = "youmi_ad"/>
2) layout. xmlCopy codeThe Code is as follows: <? 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"
>
<! -- Youmi advertisement -->
<Akai. settings. YoumiAd
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_alignParentBottom = "true"
Android: gravity = "center_horizontal"
/>
</LinearLayout>
3) because the layout carrier needs to be defined for the advertisement bar of v4 version, a LinearLayout is reloaded here, so is the custom layout.
Process layout files in YoumiAd. java. Other la s are similar and will not be described.Copy codeThe Code is as follows: 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", "ad bar switching ");
}
@ Override
Public void onReceivedAd (AdView arg0 ){
Log. I ("Youmi", "request advertisement successful ");
}
@ Override
Public void onFailedToReceivedAd (AdView arg0 ){
Log. I ("Youmi", "ad request failed ");
}
});
}
}
2. Add layout to Activity setContentView (R. layout. youmi_ad) to customize the content.
1) Add preference in xml fileCopy codeThe Code is as follows: <Preference android: layout = "@ layout/youmi_ad" android: key = "youmi_ad"/>
2) layout. xml: You need to add a ListView control with the id list. Otherwise, it cannot be run because PreferenceActivity is a List.Copy codeThe Code is as follows: <? 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: drawselectid Top = "false"
Android: layout_weight = "1"
/>
<! -- Youmi advertisement -->
<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) add setContentView (R. layout. youmi_ad) to onCreate in the Activity. Then you can use findViewById to obtain the control in the custom layout.