Android layout preferenceactivity -- insert custom Layout

Source: Internet
Author: User

Address: http://blog.csdn.net/you_and_me12/article/details/8655234


2013-03-10

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 file

<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"> <! -- 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.

Public class youmiad extends linearlayout {public youmiad (context) {super (context); Init (context) ;}public youmiad (context, attributeset attrs) {super (context, attrs); Init (context);} private void Init (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", "ad bar switching") ;}@ overridepublic void onreceivedad (adview arg0) {log. I ("youmi", "successful ad request") ;}@ overridepublic 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 file

<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.

<? 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.

End:1) after writing, go to bed.

2) continue to learn about Android

3) I am an IT programmer


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.