android layout 自定义,Android布局——Preference自定义layout的方法

导语:PreferenceActivity是一个方便设置管理的界面,但是对于界面显示来说比较单调,所以自定义布局就很有必要了。本文举例说明在Preference中自定义layout的方法。笔者是为了在设置中插入@有米v4广告条才研究了一晚上的。

正文:首先PreferenceScreen是一个xml文件于res/xml目录下,不属于layout文件。要插入layout,有两种方法。

1.使用Preference的android:@layout属性

1)xml文件中preference的添加

2)layout.xml

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:gravity="center_horizontal"

/>

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)以自定义内容

1)xml文件中preference的添加

2)layout.xml,需要添加一个ListView控件,且id为list,不然不能运行,应该是由于PreferenceActivity是一个List的原因吧。

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

>

android:id="@android:id/list"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:drawSelectorOnTop="false"

android:layout_weight="1"

/>

android:id="@+id/adLayout"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:gravity="center_horizontal"

/>

3)在Activity中的onCreate添加setContentView(R.layout.youmi_ad);然后就可以使用findViewById去获取自定义布局下的控件了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值