在做项目的时候,需要做一个preference 用来显示系统更新的条数,大概的样子如下。
从系统API中获取数字,根据数字的不同,显示不同的图片大小。
一、 自定义的prefernece代码 ,继承Preference,然后在OnBindview里面获取数据,然后更新图片。
package com.wt.preference;
import android.content.Context;
import android.content.res.Resources.NotFoundException;
import android.graphics.drawable.Drawable;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
public class HwUpdatePreference extends Preference {
private Drawable mIcon;
static final String TAG = "HwUpdatePreference";
private int num;
private TextView text;
private FrameLayout fra;
public HwUpdatePreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setLayoutResource(R.layout.preference);
}
public HwUpdatePreference(Context context, AttributeSet attrs) {
this(context, attrs, 0); //becare this states
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
// Draw the icon and update the nums on the device
fra=(FrameLayout) view.findViewById(R.id.fra);
text=(TextView) view.findViewById(R.id.title);
//get the update numbers from the system-date
num=18;//修改这里的数据,然后看到不同的数字更新。
setUpdateCount(num);
}
public void setUpdateCount(int num){
this.num=num;
text.setText(num+"");
if(num >0&& num < 10 ){
text.setBackgroundResource(R.drawable.bg_update_32);
text.setText(num+"");
}else if(num > 9 && num < 100){
text.setBackgroundResource(R.drawable.bg_update_42);
text.setText(num+"");
}else if(num > 99){
text.setBackgroundResource(R.drawable.bg_update_48);
text.setText("99+");
}else{
fra.setVisibility(View.INVISIBLE);
}
notifyChanged();
}
}
二、在activty中调用
package com.wt.preference;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Preferencetivty extends PreferenceActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.device_info_settings);
}
}
三、prefernece的布局文件 ,这个就是自定义的布局,一个线性布局,包含相对布局和帧布局,难点:用了2个Weight和一个right熟悉让,更新的数字在右边显示。
<?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="fill_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip"
android:layout_marginRight="6dip"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip"
android:layout_weight="1"
>
<TextView android:id="@+android:id/title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="@string/system_update_settings_list_item_title"
android:textAppearance="?android:attr/textAppearanceMedium"
android:ellipsize="marquee"
android:fadingEdge="horizontal" />
<TextView android:id="@+android:id/summary1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"<span style="white-space:pre"> </span>
android:layout_below="@android:id/title"
android:layout_alignLeft="@android:id/title"
android:summary="@string/system_update_settings_list_item_summary"
android:textAppearance="?android:attr/textAppearanceSmall"
android:ellipsize="end"
android:maxLines="2" />
</RelativeLayout>
<FrameLayout
android:id="@+id/fra"
<span style="white-space:pre"> </span>android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@drawable/bg_update_42"
android:gravity="center_horizontal|center_vertical"
android:text="10"
android:textSize="14dip" />
</FrameLayout>
</LinearLayout>
四、总布局 preference\res\xml\device_info_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 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. -->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/app_name">
<!-- Contributors -->
<!-- <PreferenceScreen android:key="contributors" android:title="@string/contributors_title">
<intent android:action="android.settings.TEAM" /> </PreferenceScreen> -->
<com.wt.preference.HwUpdatePreference
android:key="system_update_settings"
android:title="@string/app_name"
android:summary="@string/system_update_settings_list_item_summary">
<intent android:action="android.settings.SYSTEM_UPDATE_SETTINGS" />
</com.wt.preference.HwUpdatePreference>
</PreferenceScreen>