Android layout -- Preference custom layout Method

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.