Last active
December 29, 2015 01:59
-
-
Save goodev/7597062 to your computer and use it in GitHub Desktop.
HolerView 模式示例
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<org.goodev.NotificationLayoutItem xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" > | |
<ImageView | |
android:id="@+id/notif_icon" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentLeft="true" | |
android:layout_centerVertical="true" /> | |
<TextView | |
android:id="@+id/notif_time" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentRight="true" /> | |
<TextView | |
android:id="@+id/notif_text" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerVertical="true" | |
android:layout_toLeftOf="@id/notif_time" | |
android:layout_toRightOf="@id/notif_icon" | |
android:ellipsize="none" | |
android:maxLines="2" /> | |
</org.goodev.NotificationLayoutItem> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.content.pm.PackageManager.NameNotFoundException; | |
import android.content.res.Resources.NotFoundException; | |
import android.graphics.drawable.Drawable; | |
import android.os.SystemClock; | |
import android.text.format.DateUtils; | |
import android.util.AttributeSet; | |
import android.widget.ImageView; | |
import android.widget.RelativeLayout; | |
import android.widget.TextView; | |
public class NotificationLayoutItem extends RelativeLayout { | |
public NotificationLayoutItem(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
} | |
public NotificationLayoutItem(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public NotificationLayoutItem(Context context) { | |
super(context); | |
} | |
private ImageView mIcon; | |
private TextView mText; | |
private TextView mTime; | |
@Override | |
protected void onFinishInflate() { | |
super.onFinishInflate(); | |
mIcon = (ImageView) findViewById(R.id.notif_icon); | |
mText = (TextView) findViewById(R.id.notif_text); | |
mTime = (TextView) findViewById(R.id.notif_time); | |
} | |
// 业务逻辑代码 | |
public void setData(NotificationData data) { | |
mText.setText(data.tickerText); | |
mTime.setText(formatTime(data.time)); | |
Drawable icon; | |
try { | |
icon = getContext().getPackageManager().getResourcesForApplication(data.packageName.toString()).getDrawable(data.icon); | |
mIcon.setImageDrawable(icon); | |
} catch (NotFoundException e) { | |
e.printStackTrace(); | |
} catch (NameNotFoundException e) { | |
e.printStackTrace(); | |
} | |
} | |
private CharSequence formatTime(long time) { | |
return DateUtils.formatDateTime(getContext(), time, DateUtils.FORMAT_SHOW_TIME); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment