Android中自定义Preference
一、需求
开发横屏设备的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的更多相关文章
- [转]Android中自定义checkbox样式
android中自定义checkbox的图片和大小 其实很简单,分三步: 1.在drawable中创建文件checkbox_selector.xml: <?xml version=" ...
- Android中的Preference结构的设计与实现
本文主要通过分析源代码来分享Preference的设计和实现方式,让开发者们在今后更加顺手地使用和扩展Preference类,或者在设计其他类似的界面和功能时可以提供参考帮助. Preference概 ...
- 转--Android中自定义字体的实现方法
1.Android系统默认支持三种字体,分别为:“sans”, “serif”, “monospace 2.在Android中可以引入其他字体 . 复制代码 代码如下: <?xml versio ...
- Android中自定义ActionBar的背景色等样式style
Android中想要去自定义ActionBar的背景色等样式. [折腾过程] 1.自己找代码,发现对应的配置的地方了: AndroidManifest.xml ? 1 2 <applicatio ...
- Android中自定义veiw使用Java中的回调方法
//------------------MainActivity----中---------------------------------- import android.os.Bundle;imp ...
- android开发:Android 中自定义View的应用
大家好我们今天的教程是在Android 教程中自定义View 的学习,对于初学着来说,他们习惯了Android 传统的页面布局方式,如下代码: <?xml version="1.0&q ...
- Android中自定义组合控件
Android中自定义控件的情况非常多,一般自定义控件可以分为两种:继承控件及组合控件.前者是通过继承View或其子类,重写方法实现自定义的显示及事件处理方式:后者是通过组合已有的控件,来实现结构的简 ...
- Android中自定义ListView实现上拉加载更多和下拉刷新
ListView是Android中一个功能强大而且很常用的控件,在很多App中都有ListView的下拉刷新数据和上拉加载更多这个功能.这里我就简单记录一下实现过程. 实现这个功能的方法不止一个,Gi ...
- Android中自定义View和自定义动画
Android FrameWork 层给我们提供了很多界面组件,但是在实际的商业开发中这些组件往往并不能完全满足我们的需求,这时候我们就需要自定义我们自己的视图和动画. 我们要重写系统的View就必须 ...
随机推荐
- systemverilog soft constraint
1.class my_item; rand bit constrainted_random; rand bit usually_one; endclass class my_generator; my ...
- Cocos2dx开发之运行与渲染流程分析
学习Cocos2dx,我们都知道程序是由 AppDelegate 的方法 applicationDidFinishLaunching 开始,在其中做些必要的初始化,并创建运行第一个 CCScene 即 ...
- 通信导论-IP数据网络基础(4)
IP地址的编址方法--IP地址+掩码地址=网络地址 分类的IP地址 每一类地址都由两个固定长度的字段组成,其中一个字段是网络号 net-id,标志主机或路由器所连接到的网络,另一个字段则是主机号 ho ...
- ROS零门槛学渣教程系列(二)——Linux常用指令:mkdir、tar、 unzip、cp、scp、mv、rm、find、apt、ssh
Linux常用指令通过上一教程,我们获得了ubuntu系统.Linux是一个很大的领域.但不要紧张,我们一步步来就是了,跟着教程,需要能用到新知识,会提前介绍给大家.下面学习几个常用的Linux指令. ...
- 处理ajax数据;数据渲染
当我们用ajax把数据拿到前台,该如何渲染到页面,有以下几种方式: 一:使用字符串拼接的方法 声明一个空变量,然后拼接 var st=""; st+="<div&g ...
- 从Excel获取请求体
Excel文件 .py文件---------------------- import xlrdimport re def fetch_body(path,sheet,name,adict): ...
- java整数溢出问题及提升为long型
整数溢出问题 Java 中的 int 用 32 位表示,正数最大值的情况,首位是 0,其他位都可以是 1(就是 2^31-1).但是如果正数过大了,例如 2^31,计算机不得不把首位变成 1,并且计算 ...
- 如何配置nginx屏蔽恶意域名解析指向《包含隐藏nginx版本号》
恶意域名指向: 比如,有一个垃圾域名将解析指向到了你们服务器的IP,一般多一个解析可能不会有什么问题,但是现在全民备案时期,可能你的运营商会联系你,说你们的域名没备案,可能会封你们的80端口,然后会导 ...
- MR执行环境有两种
本地测试环境(windows): 在windows的hadoop目录bin目录有一个winutils.exe 1.在windows下配置hadoop的环境变量 2.拷贝debug工具(winutils ...
- [Git] 拉开发分支的代码报错
Git拉开发分支的代码报错: fatal: The remote end hung up unexpectedly fatal: early EOF fatal: index-pack failed ...