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就必须 ...
随机推荐
- Ubuntu16.04 藍牙連上,但是聲音裏面找不到設備
解決辦法: 1. sudo apt-get install blueman bluez* 2. sudo vim /etc/pulse/default.pa 注釋掉下面的代碼: #.ifexists ...
- 用BlockingQueue实现的简单发布订阅模式
- MySQL主主
MySQL双主(主主)架构方案 在企业中,数据库高可用一直是企业的重中之重,中小企业很多都是使用mysql主从方案,一主多从,读写分离等,但是单主存在单点故障,从库切换成主库需要作改动.因此,如果 ...
- crontab学习
概念 日志路径 /var/log/cron 配置路径 vi /etc/crontab 参考 https://www.cnblogs.com/kenshinobiy/p/7685229.html 问题 ...
- Android中竖线随内容高度变化而变化的问题和解决办法
项目中要求显示竖线,并且竖线高度不确定,竖线的高度要随着内容的变化而变化.不能使用match_parent 充满,也不能在布局中写死,此时使用 android:layout_height=" ...
- Spring @Value取值为null或@Autowired注入失败
@Value 用于注入.properties文件中定义的内容 @Autowired 用于装配bean 用法都很简单,很直接,但是稍不注意就会出错.下面就来说说我遇到的问题. 前两天在项目中遇到了一个问 ...
- python 练习3
简单计算器的实现: 计算:1-2*((60-30+(-40.0/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))' #!usr/bin/en ...
- Linux中硬链接和软链接的区别
看了这篇文章之后,豁然开朗.直接放链接,感谢作者的分享. https://www.ibm.com/developerworks/cn/linux/l-cn-hardandsymb-links/#ico ...
- mysql 外网访问
--修改用户Root 可以外网访问 update user set host='%' where user='root'; --查询修改结果 select user,host from user; - ...
- 数据库基础 RDBMS、NoSQL