一、需求

开发横屏设备的app时,发现preference显示的都是上下结构,因此需要自定义preference实现横屏显示。

二、layout实现

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"> <TextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="@dimen/space_horizontal"
android:layout_gravity="center"
android:gravity="start"
android:textColor="@color/text_font_black"
android:textSize="@dimen/font_size_prompt"/> <CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/space_horizontal"
android:layout_gravity="center"
android:button="@drawable/selection_switch_check_box_background"
android:visibility="gone"/> <TextView
android:id="@+id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/space_horizontal"
android:layout_gravity="center"
android:gravity="end"/> </LinearLayout>

三、EditTextPreference

public class EditTextPreferenceFix extends EditTextPreference {

    public EditTextPreferenceFix(Context context, AttributeSet attrs) {
super(context, attrs);
} @Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
getEditText().clearFocus();
hideSysInput();
} @Override
protected View onCreateView(ViewGroup parent) {
super.onCreateView(parent);
return LayoutInflater.from(getContext()).inflate(R.layout.layout_preference,
parent, false);
} @Override
protected void onBindView(View view) {
super.onBindView(view);
TextView titleView = (TextView) view.findViewById(R.id.title);
titleView.setText(getTitle());
TextView summaryView = (TextView) view.findViewById(R.id.summary);
summaryView.setText(getSummary());
} private void hideSysInput() {
Window window = ((Activity) getContext()).getWindow();
View contentView = window.findViewById(Window.ID_ANDROID_CONTENT); if (contentView.getWindowToken() != null) {
ViewUtils.hideSystemKeyboard(getContext(), contentView);
}
}
}

四、ListPreference

public class ListPreferenceFix extends ListPreference {

    public ListPreferenceFix(Context context, AttributeSet attrs) {
super(context, attrs);
} @Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
hideSysInput();
} @Override
protected View onCreateView(ViewGroup parent) {
super.onCreateView(parent);
return LayoutInflater.from(getContext()).inflate(R.layout.layout_preference,
parent, false);
} @Override
protected void onBindView(View view) {
super.onBindView(view);
TextView titleView = (TextView) view.findViewById(R.id.title);
titleView.setText(getTitle());
TextView summaryView = (TextView) view.findViewById(R.id.summary);
summaryView.setText(getSummary());
} private void hideSysInput() {
Window window = ((Activity) getContext()).getWindow();
View contentView = window.findViewById(Window.ID_ANDROID_CONTENT); if (contentView.getWindowToken() != null) {
ViewUtils.hideSystemKeyboard(getContext(), contentView);
}
}
}

五、CheckBoxPreference

public class CheckBoxPreferenceFix extends CheckBoxPreference {

    public CheckBoxPreferenceFix(Context context, AttributeSet attrs) {
super(context, attrs);
} @Override
protected View onCreateView(ViewGroup parent) {
super.onCreateView(parent);
return LayoutInflater.from(getContext()).inflate(R.layout.layout_preference,
parent, false);
} @Override
protected void onBindView(View view) {
super.onBindView(view);
TextView titleView = (TextView) view.findViewById(R.id.title);
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox);
TextView summaryView = (TextView) view.findViewById(R.id.summary);
titleView.setText(getTitle());
checkBox.setVisibility(View.VISIBLE);
checkBox.setChecked(isChecked());
summaryView.setText(isChecked() ? getSummaryOn() : getSummaryOff());
checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
summaryView.setText(isChecked ? getSummaryOn() : getSummaryOff());
setChecked(isChecked);
});
}
}

六、XML文件

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <com.pax.view.ListPreferenceFix
android:defaultValue="@string/wifi"
android:dialogTitle="@string/commParam_menu_comm_mode_choose"
android:entries="@array/commParam_menu_comm_mode_list_entries"
android:entryValues="@array/commParam_menu_comm_mode_values_list_entries"
android:key="@string/COMM_TYPE"
android:negativeButtonText="@null"
android:positiveButtonText="@null"
android:title="@string/commParam_menu_comm_mode"/>
<!-- 打开一个sub screen 移动网络-->
<PreferenceCategory
android:persistent="false"
android:title="@string/communication_menu_mobile">
<com.pax.view.CheckBoxPreferenceFix
android:defaultValue="true"
android:key="@string/MOBILE_KEEP_ALIVE"
android:summaryOff="@string/no"
android:summaryOn="@string/yes"
android:title="@string/commParam_menu_mobile_wifi_keep_alive"/>
<com.pax.view.EditTextPreferenceFix
android:capitalize="words"
android:key="@string/MOBILE_TEL_NO"
android:maxLines="1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/commParam_menu_mobile_dial_no"/>
    </PreferenceCategory>
</PreferenceScreen>
 

Android中自定义Preference的更多相关文章

  1. [转]Android中自定义checkbox样式

    android中自定义checkbox的图片和大小   其实很简单,分三步: 1.在drawable中创建文件checkbox_selector.xml: <?xml version=" ...

  2. Android中的Preference结构的设计与实现

    本文主要通过分析源代码来分享Preference的设计和实现方式,让开发者们在今后更加顺手地使用和扩展Preference类,或者在设计其他类似的界面和功能时可以提供参考帮助. Preference概 ...

  3. 转--Android中自定义字体的实现方法

    1.Android系统默认支持三种字体,分别为:“sans”, “serif”, “monospace 2.在Android中可以引入其他字体 . 复制代码 代码如下: <?xml versio ...

  4. Android中自定义ActionBar的背景色等样式style

    Android中想要去自定义ActionBar的背景色等样式. [折腾过程] 1.自己找代码,发现对应的配置的地方了: AndroidManifest.xml ? 1 2 <applicatio ...

  5. Android中自定义veiw使用Java中的回调方法

    //------------------MainActivity----中---------------------------------- import android.os.Bundle;imp ...

  6. android开发:Android 中自定义View的应用

    大家好我们今天的教程是在Android 教程中自定义View 的学习,对于初学着来说,他们习惯了Android 传统的页面布局方式,如下代码: <?xml version="1.0&q ...

  7. Android中自定义组合控件

    Android中自定义控件的情况非常多,一般自定义控件可以分为两种:继承控件及组合控件.前者是通过继承View或其子类,重写方法实现自定义的显示及事件处理方式:后者是通过组合已有的控件,来实现结构的简 ...

  8. Android中自定义ListView实现上拉加载更多和下拉刷新

    ListView是Android中一个功能强大而且很常用的控件,在很多App中都有ListView的下拉刷新数据和上拉加载更多这个功能.这里我就简单记录一下实现过程. 实现这个功能的方法不止一个,Gi ...

  9. Android中自定义View和自定义动画

    Android FrameWork 层给我们提供了很多界面组件,但是在实际的商业开发中这些组件往往并不能完全满足我们的需求,这时候我们就需要自定义我们自己的视图和动画. 我们要重写系统的View就必须 ...

随机推荐

  1. AttributeError: 'LoginForm' object has no attribute 'is_bound' , object has no attribute 'is_bound'

    'LoginForm' object has no attribute 'is_bound' 可能原因: 啥子问题?? 都是 jquery.js 文件.....

  2. gitlab-ci.xml:script config should be a string or an array of strings

    The following command in a job script: STATUS_ID=$(grep -Eo "Status Code [0-9]+: Done" som ...

  3. SQLServer导入导出命令报错

    错误描述: SQL Server阻止了对组件‘xp_cmdshell’的过程‘sys.xp_cmdshell’的访问.因为此组件已作为此服务嚣安全配置的一部分而被关闭. 系统管理员可以通过使用sp_c ...

  4. js如何获取点击<li>标签里的内容值

    路:为li对象添加单击事件→事件触发后利用innerHTML获取li的文本.实例演示如下: 1.HTML结构 <ul id="test"> <li>Glen ...

  5. MySQL数据库开发常见问题及几点优化!

    从一下三个方面考虑: 库表设计 慢 SQL 问题 误操作.程序 bug 时怎么办 一.库表设计 1.1.引擎选择 在 MySQL5.1 中,引入了新的插件式存储引擎体系结构,允许将存储引擎加载到正在运 ...

  6. Swoole 网络通信协议,固定包头。

    网络通信过程中,可能会出现分包和合包的情况.具体情况如https://wiki.swoole.com/wiki/page/484.html文档所讲的.这里测试了下固定包头的协议.示例代码如下 1.解包 ...

  7. VNF网络性能提升解决方案及实践

    VNF网络性能提升解决方案及实践 2016年7月 作者:    王智民 贡献者:     创建时间:    2016-7-20 稳定程度:    初稿 修改历史 版本 日期 修订人 说明 1.0 20 ...

  8. 递归求6的阶乘(考虑int类型溢出)

    编码 public class Factorial { public static void main(String[] args) { System.out.println(fac(6)); } p ...

  9. docker--容器和镜像的导入导出及部署

    一.镜像导出 save 1.查看镜像 docker images 2.导出镜像 docker save -o test.tar image_name 或 docker save image_name ...

  10. 20170529计划---统计业务量并生成EXCEL通过邮件发送

    每个月都要统计这些业务量的东东,烦死了,赶紧通过python写一个来搞定吧,三天搞定吧,未完待续哈. 2017-5-29 19:50粗略地做了一个思维导图哈 终于第三天完成啦 #encoding=ut ...