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

 更新时间:2013年06月04日 17:24:39   作者:  
PreferenceActivity是一个方便设置管理的界面,但是对于界面显示来说比较单调,所以自定义布局就很有必要了,下面与大家分享下Preference中自定义layout的方法
导语:PreferenceActivity是一个方便设置管理的界面,但是对于界面显示来说比较单调,所以自定义布局就很有必要了。本文举例说明在Preference中自定义layout的方法。笔者是为了在设置中插入@有米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)以自定义内容
1)xml文件中preference的添加
复制代码 代码如下:

<Preference android:layout="@layout/youmi_ad" android:key="youmi_ad"/>

2)layout.xml,需要添加一个ListView控件,且id为list,不然不能运行,应该是由于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去获取自定义布局下的控件了。

相关文章

  • android实现一个图片验证码倒计时功能

    android实现一个图片验证码倒计时功能

    本文通过实例代码给大家介绍了android实现一个图片验证码倒计时功能,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2017-11-11
  • Android自定义view实现仿抖音点赞效果

    Android自定义view实现仿抖音点赞效果

    这篇文章主要介绍了Android自定义view实现仿抖音点赞效果,代码简单易懂非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-05-05
  • Android 利用ImageView属性实现选中和未选中效果

    Android 利用ImageView属性实现选中和未选中效果

    这篇文章主要介绍了Android巧用ImageView属性实现选中和未选中效果,实现思路通常我们会选择在布局里加个ImageView,然后通过代码层面加个判断去让ImageView加载不同状态的图片,需要的朋友可以参考下
    2023-06-06
  • Android7.0版本影响开发的改进分析

    Android7.0版本影响开发的改进分析

    这篇文章主要介绍了Android7.0版本影响开发的改进,总结分析了Android7.0版本中比较常见的开发注意事项与操作技巧,需要的朋友可以参考下
    2017-11-11
  • Flutter开发实现底部留言板

    Flutter开发实现底部留言板

    这篇文章主要为大家详细介绍了Flutter开发实现底部留言板,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • Android HttpURLConnection.getResponseCode()错误解决方法

    Android HttpURLConnection.getResponseCode()错误解决方法

    在使用HttpURLConnection.getResponseCode()的时候直接报错是IOException错误,一直想不明白,同一个程序我调用了两次,结果有一个链接一直OK,另一个却一直报这个错误
    2013-06-06
  • Android 8.1隐藏状态栏图标的实例代码

    Android 8.1隐藏状态栏图标的实例代码

    这篇文章主要介绍了Android 8.1隐藏状态栏图标,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-04-04
  • Android自定义ViewPager指示器

    Android自定义ViewPager指示器

    这篇文章主要为大家详细介绍了Android自定义ViewPager指示器的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-11-11
  • Android判断当前App是在前台还是在后台

    Android判断当前App是在前台还是在后台

    这篇文章主要为大家详细介绍了Android判断当前App是在前台还是在后台的方法,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • Android自定义加载圈动画效果

    Android自定义加载圈动画效果

    这篇文章主要为大家详细介绍了Android自定义加载圈动画效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08

最新评论