代码图片布局创意等等所有的一切都归原创者所有
我已经分不清谁是原创
如要用于商业,请遵守相关法则
主要的代码就是SyncStatusPanel.java
山寨的还不够干净利落,有些代码可能无用
这个类代码很少,但是还是可以学到些基本知识
比如为什么要用Runnable来更新UI
adaptSizeToDensity方法的目的
用XML来定义背景图形
RelativeLayout基本知识
等等等等
调用方法如下:
直接上图
山寨:
[img]http://dl.iteye.com/upload/attachment/408777/63157d84-4c78-3872-9dda-68752a5001c2.png[/img]
原图:
https://android-client.forge.funambol.org/
[img]https://android-client.forge.funambol.org/cn-project-pages/images/home.png[/img]
估计我这直接叫抄袭吧,姑且叫它学习
我已经分不清谁是原创
如要用于商业,请遵守相关法则
主要的代码就是SyncStatusPanel.java
山寨的还不够干净利落,有些代码可能无用
package org.xkit.android.demo;
import android.app.Activity;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
@SuppressWarnings("unused")
public class SyncStatusPanel extends RelativeLayout {
private class SetIconUIThread implements Runnable {
private int image;
public SetIconUIThread() {
}
public void run() {
if (image != 0) {
sourceIconView.setImageResource(image);
} else {
sourceIconView.setImageResource(0);
}
}
public void setIcon(int image) {
this.image = image;
}
}
private class SetStatusIconUIThread implements Runnable {
private int image;
public SetStatusIconUIThread() {
}
public void run() {
if (image != 0) {
statusIconView.setImageResource(image);
} else {
statusIconView.setImageResource(0);
}
}
public void setIcon(int image) {
this.image = image;
}
}
private class SetStatusStringUIThread implements Runnable {
private String text;
public SetStatusStringUIThread() {
}
public void run() {
statusTextView.setText(text);
}
public void setText(String value) {
this.text = value;
}
}
private class SetTitleUIThread implements Runnable {
private String title;
public SetTitleUIThread() {
}
public void run() {
titleTextView.setText(title);
}
public void setTitle(String title) {
this.title = title;
}
}
protected static final int BOTTOM_PADDING = 8;
private static final int DISABLED_TEXT_COLOR = 0xFF7F7F7F;
private static final int ENABLED_TEXT_COLOR = 0xFF000000;
protected static final int SOURCE_ICON_LEFT_PADDING = 4;
protected static final int SOURCE_ICON_RIGHT_PADDING = 4;
private static final int STATUS_FONT_SIZE = 12;
protected static final int STATUS_ICON_R_PADDING = 4;
protected static final int STATUS_L_PADDING = 12;
private static final int TITLE_FONT_SIZE = 20;
protected static final int TITLE_L_PADDING = 5;
protected static final int TOP_PADDING = 8;
protected Activity activity;
protected SetIconUIThread setIconUIThread = new SetIconUIThread();
protected SetStatusIconUIThread setStatusIconUIThread = new SetStatusIconUIThread();
protected SetStatusStringUIThread setStatusStringUIThread = new SetStatusStringUIThread();
protected SetTitleUIThread setTitleUIThread = new SetTitleUIThread();
protected ImageView sourceIconView;
protected ImageView statusIconView;
protected TextView statusTextView;
protected TextView titleTextView;
// 每个元素的ID
protected final int D1 = 0X514001, D2 = 0X514002, D3 = 0X514003,
D4 = 0X514004;
public SyncStatusPanel(Activity activity) {
super(activity);
this.activity = activity;
statusIconView = createStatusIcon(activity);
sourceIconView = createSourceIcon(activity);
titleTextView = createTitleText(activity);
statusTextView = createStatusText(activity);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.CENTER_VERTICAL);
params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
addView(sourceIconView, params1);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.CENTER_VERTICAL);
params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
addView(statusIconView, params2);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params3
.addRule(RelativeLayout.RIGHT_OF,
sourceIconView.getId());
params3.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params3.addRule(RelativeLayout.LEFT_OF, statusIconView.getId());
addView(titleTextView, params3);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params4
.addRule(RelativeLayout.RIGHT_OF,
sourceIconView.getId());
params4.addRule(RelativeLayout.BELOW, titleTextView.getId());
params4.addRule(RelativeLayout.LEFT_OF, statusIconView.getId());
addView(statusTextView, params4);
setBackgroundResource(R.drawable.sync_status_panel);
}
protected int adaptSizeToDensity(int size) {
return (int) (size * getContext().getResources()
.getDisplayMetrics().density);
}
protected ImageView createSourceIcon(Activity activity) {
ImageView sourceIconView = new ImageView(activity);
sourceIconView.setPadding(
adaptSizeToDensity(SOURCE_ICON_LEFT_PADDING),
adaptSizeToDensity(TOP_PADDING),
adaptSizeToDensity(SOURCE_ICON_RIGHT_PADDING),
adaptSizeToDensity(BOTTOM_PADDING));
sourceIconView.setId(D1);
return sourceIconView;
}
protected ImageView createStatusIcon(Activity activity) {
ImageView statusIconView = new ImageView(activity);
statusIconView.setAdjustViewBounds(true);
statusIconView.setMaxHeight(adaptSizeToDensity(32));
statusIconView.setMaxWidth(adaptSizeToDensity(32));
statusIconView.setId(D2);
return statusIconView;
}
protected TextView createStatusText(Activity activity) {
TextView statusTextView = new TextView(activity);
statusTextView.setPadding(adaptSizeToDensity(STATUS_L_PADDING),
0, 0, 0);
statusTextView.setTextSize(STATUS_FONT_SIZE);
statusTextView.setTextColor(ENABLED_TEXT_COLOR);
statusTextView.setBackgroundColor(android.R.color.black);
statusTextView.setId(D3);
return statusTextView;
}
protected TextView createTitleText(Activity activity) {
TextView titleTextView = new TextView(activity);
titleTextView.setTextSize(TITLE_FONT_SIZE);
titleTextView.setPadding(adaptSizeToDensity(TITLE_L_PADDING),
adaptSizeToDensity(TOP_PADDING), 0, 0);
titleTextView.setTextColor(ENABLED_TEXT_COLOR); // COLOR
titleTextView.setId(D4);
return titleTextView;
}
public void setIcon(int image) {
setIconUIThread.setIcon(image);
activity.runOnUiThread(setIconUIThread);
}
public void setStatusIcon(int image) {
setStatusIconUIThread.setIcon(image);
activity.runOnUiThread(setStatusIconUIThread);
}
public void setStatusString(String value) {
setStatusStringUIThread.setText(value);
activity.runOnUiThread(setStatusStringUIThread);
}
public void setTitle(String value) {
setTitleUIThread.setTitle(value);
activity.runOnUiThread(setTitleUIThread);
}
}
这个类代码很少,但是还是可以学到些基本知识
比如为什么要用Runnable来更新UI
adaptSizeToDensity方法的目的
用XML来定义背景图形
RelativeLayout基本知识
等等等等
调用方法如下:
LinearLayout lll = (LinearLayout) findViewById(R.id.boxes);
LinearLayout.LayoutParams ll1Params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
SyncStatusPanel ssp = new SyncStatusPanel(this); // 这个是我们自定义的
ssp.setIcon(R.drawable.icon_calendar);
ssp.setTitle("日程安排");
ssp.setStatusString("上次同步在 2010-01-01 12:23:32");
ssp.setStatusIcon(R.drawable.icon_complete);
lll.addView(ssp, ll1Params);
直接上图
山寨:
[img]http://dl.iteye.com/upload/attachment/408777/63157d84-4c78-3872-9dda-68752a5001c2.png[/img]
原图:
https://android-client.forge.funambol.org/
[img]https://android-client.forge.funambol.org/cn-project-pages/images/home.png[/img]
估计我这直接叫抄袭吧,姑且叫它学习