一、需求

开发横屏设备的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. mysql mycat 中间件安装与使用

    一,什么是mycat 一个彻底开源的,面向企业应用开发的大数据库集群 支持事务.ACID.可以替代MySQL的加强版数据库 一个可以视为MySQL集群的企业级数据库,用来替代昂贵的Oracle集群 一 ...

  2. 2018-2019-2 20175126谢文航 实验二《Java面向对象程序设计》实验报告

    一.实验报告封面 课程:Java程序设计 班级:1751 班 姓名:谢文航 学号:20175126 指导教师:娄嘉鹏 实验日期:2019年4月17日 实验时间:--- 实验序号:实验二 实验名称:Ja ...

  3. 算法练习LeetCode初级算法之其他

    位1的个数 解法一: class Solution { // you need to treat n as an unsigned value public int hammingWeight(int ...

  4. bootstrap模态框手动关闭遮盖层不消失

    模态框中 加载了一个子页面 子页面中调教表单之后想根据执行结果手动关闭模态框,最初尝试了以下几种方案: 1.$("#myModal").modal('hide');//模态框关闭 ...

  5. 有关Lambda的一些思考

    问题: What do lambda expressions do? Can we write all functions as lambda expressions? In what cases a ...

  6. linux内存黑洞

    1.问题 k8s集群中node节点的内存使用率居高不下,使用率达到90%多.通过以下命令可以发现此虚拟机分配的内存为15g,但是用户进程使用的内存总共为7个多g,并且slab和pageTables使用 ...

  7. Mysql运行状态查询命令及调优详解

    (转载自点击打开链接) MySQL运行状态及调优(一) 一.查看MySQL运行情况SHOW STATUS; 二.查看INNODB数据库引擎运行状态SHOW ENGINE INNODB STATUS; ...

  8. VB中将类标记为可序列化

    引用名空间: Imports SystemImports System.Runtime.Serialization 在类前加特性: <Serializable> 更多内容: https:/ ...

  9. Numpy 矩阵库(Matrix)

    Numpy 中包含了一个矩阵库 numpy.matlib, 该模块中的函数返回的是一个矩阵, 而不是 ndarray 对象. 一个 m * n de 矩阵是一个 有 m 行(row) n 列(colu ...

  10. 4-2 requests库使用

    打开自己的编辑器 创建一个py文件 这里是首先调用requests库 调用requests  最后打印出response