android:修改preference中view属性

android开发包preference提供了我们一些方面的设置工具,PreferenceActivity,Preference,PreferenceScreen,CheckBoxPreference等,利用他们可以方便的建立应用程序的属性设置。如图:

  

  有些时候,我们并不需要系统给我们的这么单调的ui,我们通常会修改一些属性,比如view的背景,字体的属性等,那么这个时候有两种选择:

  1.针对单个应用程序,定义一个cutom的layout,当然这个layout跟系统的layout元素要一致(否则你怎么改呢?),然后在preference.xml(文件名你自己随便取)android:layout添加你定一个 的layout就可以了。

  2.针对整个android系统,修改preference相关的属性,那么所有的用到PreferenceActivity的界面都会相应的改变。这里只需要修改相应的系统layout文件(在framework/base/core/res/res/layout),比如preference_category.xml等。

  1.先说说地一种吧,比较常用到的。

  首先,定义你的layout文件,这个layoutn你直接到framework拷下来,这里标记为custom_preference.xml

复制代码
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent" android:layout_height="wrap_content"
 3     android:minHeight="?android:attr/listPreferredItemHeight"
 4     android:gravity="center_vertical" android:paddingRight="?android:attr/scrollbarSize">
 5 
 6     <RelativeLayout android:layout_width="wrap_content"
 7         android:layout_height="wrap_content" android:layout_marginLeft="15dip"
 8         android:layout_marginRight="6dip" android:layout_marginTop="6dip"
 9         android:layout_marginBottom="6dip" android:layout_weight="1">
10 
11         <TextView android:id="@+android:id/title"
12             android:layout_width="wrap_content" android:layout_height="wrap_content"
13             android:singleLine="true" android:textAppearance="?android:attr/textAppearanceLarge"
14             android:ellipsize="marquee" android:fadingEdge="horizontal"
15             android:textColor="#475ad7"/>
16 
17         <TextView android:id="@+android:id/summary"
18             android:layout_width="wrap_content" android:layout_height="wrap_content"
19             android:layout_below="@android:id/title" android:layout_alignLeft="@android:id/title"
20             android:textAppearance="?android:attr/textAppearanceSmall"
21             android:textColor="#5ad747"/>
22 
23     </RelativeLayout>
24 
25     <!-- Preference should place its actual preference widget here. -->
26     <LinearLayout android:id="@+android:id/widget_frame"
27         android:layout_width="wrap_content" android:layout_height="match_parent"
28         android:gravity="center_vertical" android:orientation="vertical"/>
29 
30 </LinearLayout>
复制代码

然后,你在些preference.xml文件的时候,应用到这个layout文件。

复制代码
1 <?xml version="1.0" encoding="utf-8"?>
2 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
3     <CheckBoxPreference    android:key="checkbox_key"
4         android:title="checkbox_title"
5         android:summary="checkbox_summery"
6         android:defaultValue="true"
7         android:layout="@layout/custom_preference_layout"></CheckBoxPreference>
8 </PreferenceScreen>
复制代码

ok,到这里,你就像平时一样使用这个preference.xml文件就好了,你会发现字体的颜色成功改变了

  

2.第二种情况,需要搭建android系统的编译环境,这个环境怎么搭建这里就不说了。


直接到系统layout文件(在framework/base/core/res/res/layout),修改preference.xml,这里只是针对上面那个例子,你当然也可以修改其他的preference相应的文件。preference.xml文件的内容就是第一种情况的custom_preference.xml文件的内容,这里就不重复了。

修改后重新mka下系统,重启模拟器,ok,就可以看到第一种方法一样的效果了。



mCustomPreference.setLayoutResource(R.layout.bluetooth_checkbox_preference_layout);

  • 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、付费专栏及课程。

余额充值