Android 自定义 Preference

有些时候系统提供的 Preference 不满足我们的要求的时候,我们就需要自己定制了。现在产品要求 ChekBoxPreference? 的 summary 的颜色要能动态改变,在关闭的时候是默认颜色,在开启的时候变成红色。现在我们就可以自己定制啦。

简单的修改 xml

先说说简单的情况。如果字体颜色只是静态的话,可以不用改代码,改改 layout xml 就好了。系统自己的 xml 是 framework/base/core/res/res/layout/preference.xml 。把这个文件复制,然后在其基础上改改 text 的属性就好了,然后在使用  CheckBoxPreference?  的时候指定自己定制的 xml(这里我叫 custom_preference.xml)。
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2006 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<!-- Layout for a Preference in a PreferenceActivity. The
Preference is able to place a specific widget for its particular
type in the "widget_frame" layout. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize">

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginRight="6dip"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip"
android:layout_weight="1">

<TextView android:id="@+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:ellipsize="marquee"
android:fadingEdge="horizontal" />

// 就是在这里改啦,加一个 textColor 属性
<TextView android:id="@+android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@android:id/title"
android:layout_alignLeft="@android:id/title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#FFFF0000" 
android:maxLines="4" />

</RelativeLayout>

<!-- Preference should place its actual preference widget here. -->
<LinearLayout android:id="@+android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" />

</LinearLayout>

// 改好后,在用 CheckBoxPreference 的时候,指定自己的 xml layout
<CheckBoxPreference android:key="lock_screen" 
android:title="@string/settings_lock_screen_title" 
android:defaultValue="false" 
android:layout="@layout/custom_preference"
android:summaryOff="@string/settings_lock_screen_off_summary" 
android:summaryOn="@string/settings_lock_screen_on_summary" 
android:summary="@string/settings_lock_screen_off_summary">
</CustomCheckBoxPreference>

扩展代码

如果需要复杂点的功能,就需要继承  CheckBoxPreference?  ,然后自己定制代码了。这里以动态的改字体颜色为例:继承  CheckBoxPreference?  后,重载父类的 onBindView 函数(这个函数好像是在将数据显示到视图上的时候调用的):

public class CustomCheckBoxPreference extends CheckBoxPreference {
        
        public CustomCheckBoxPreference(Context context, AttributeSet attrs,
        int defStyle) {
                super(context, attrs, defStyle);
                // TODO Auto-generated constructor stub
        }
        
        @Override
        protected void onBindView(View view) {
                super.onBindView(view);
                
                boolean isChecked = isChecked();
                Resources res = view.getResources();
                
                TextView summaryView = (TextView)view.findViewById(com.android.internal.R.id.summary);
                if (summaryView != null) {
                        if (isChecked) {
                                summaryView.setTextColor(res.getColor(R.color.red));
                        } else {
                                //summaryView.setTextAppearance(getContext(), com.android.internal.R.attr.textAppearanceSmall);
                                summaryView.setTextColor(res.getColor(com.android.internal.R.color.secondary_text_light));
                        }
                }
        }
}

写好自己的类之后,在用的时候把原来的 CheckBoxPreference? 换成自己的类就可以了:

<cn.fmsoft.launcher2.CustomCheckBoxPreference android:key="lock_screen" 
android:title="@string/settings_lock_screen_title" 
android:defaultValue="false" 
android:summaryOff="@string/settings_lock_screen_off_summary" 
android:summaryOn="@string/settings_lock_screen_on_summary" 
android:summary="@string/settings_lock_screen_off_summary">
</cn.fmsoft.launcher2.CustomCheckBoxPreference>

参考

参考此处: Preference 使用小结
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个自定义Preference布局的示例代码,包含一个Button按钮并实现了点击事件: 首先,在res/layout目录下创建一个名为custom_preference.xml的布局文件,代码如下: ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="?android:attr/listPreferredItemHeight" android:gravity="center_vertical" android:paddingEnd="?android:attr/scrollbarSize"> <!-- Preference icon --> <ImageView android:id="@+android:id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginEnd="10dp" android:layout_gravity="center_vertical" android:contentDescription="@null" /> <!-- Preference title and summary --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+android:id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLargeInverse" android:textColor="?android:attr/textColorPrimaryInverse" android:singleLine="true" android:ellipsize="end" android:textStyle="bold" android:paddingTop="5dp" /> <TextView android:id="@+android:id/summary" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="?android:attr/textColorSecondaryInverse" android:maxLines="3" android:ellipsize="end" android:paddingBottom="5dp" /> </LinearLayout> <!-- Custom button --> <Button android:id="@+id/custom_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Custom Button" android:layout_marginEnd="10dp" android:onClick="onButtonClicked" /> </LinearLayout> ``` 其中,我们添加了一个Button按钮,设置了其ID为custom_button,并在其中添加了一个onClick属性,指向一个名为onButtonClicked的方法。 接着,在我们的Preference类中,重写onBindViewHolder方法,以及实现onButtonClicked方法,完整代码如下: ```java public class CustomPreference extends Preference { public CustomPreference(Context context, AttributeSet attrs) { super(context, attrs); setLayoutResource(R.layout.custom_preference); } @Override public void onBindViewHolder(PreferenceViewHolder holder) { super.onBindViewHolder(holder); // Get the custom button view Button button = (Button) holder.findViewById(R.id.custom_button); // Set the click listener for the button button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Do something when the button is clicked Toast.makeText(getContext(), "Custom button clicked", Toast.LENGTH_SHORT).show(); } }); } // Custom button click handler public void onButtonClicked(View view) { // Do something when the button is clicked Toast.makeText(getContext(), "Custom button clicked", Toast.LENGTH_SHORT).show(); } } ``` 在该类中,我们重写了onBindViewHolder方法,通过ViewHolder获取了我们自定义布局中的Button,并设置了其点击事件。同时,我们还实现了一个名为onButtonClicked的方法,用于处理Button的点击事件。 最后,在我们的PreferenceActivity或PreferenceFragment中,添加我们自定义Preference: ```xml <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="Custom Preference Category"> <com.example.myapplication.CustomPreference android:key="custom_preference" android:title="Custom Preference" android:summary="This is a custom preference with a button" /> </PreferenceCategory> </PreferenceScreen> ``` 这样就完成了一个自定义Preference布局,并添加了一个Button按钮并实现了点击事件的示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值