我正在尝试创建自定义通知状态栏,例如 Samsung Galaxy S3股票音乐播放器。我正在使用 RemoteViews 进行自定义布局。但是我的textview组件无法在选取框模式下工作。
我的代码如下。我犯错的地方?我花了很多时间来解决这个问题,但我不是运气。
三星Galaxy S3股票音乐播放器通知栏:
这是我的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="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="64dip"
android:layout_height="64dip"
android:orientation="vertical" >
<ImageView
android:id="@+id/imgViewIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textViewNotifPlaylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="@string/app_name"
android:textColor="#0094FF" >
<requestFocus />
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="64dip"
android:layout_marginLeft="3dip"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/imgBtnNotifPrevious"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_marginRight="20dp"
android:background="@drawable/button_circle"
android:contentDescription="@string/media_previous"
android:src="@drawable/media_btn_previous" />
<ImageButton
android:id="@+id/imgBtnNotifPlayPause"
android:layout_width="40dip"
android:layout_height="40dip"
android:background="@drawable/button_circle"
android:contentDescription="@string/media_play"
android:src="@drawable/media_btn_play" />
<ImageButton
android:id="@+id/imgBtnNotifNext"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_marginLeft="20dp"
android:background="@drawable/button_circle"
android:contentDescription="@string/media_next"
android:src="@drawable/media_btn_next" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textViewNotifItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:scrollHorizontally="false"
android:focusable="true"
android:focusableInTouchMode="true"
android:duplicateParentState="true"
android:text="This is the long text and I want to this marquee mode." >
<requestFocus
android:focusable="true"
android:focusableInTouchMode="true"
android:duplicateParentState="true" />
</TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
这是我的java代码:
private void showNotification()
{
final NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent piContent = ...;
PendingIntent piPrevious = ...;
PendingIntent piPausePlay = ...;
PendingIntent piNext = ...;
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(false)
.setTicker("This is ticker text")
.setContentTitle(getString(R.string.app_name))
.setContentText("This is content text")
.setContentIntent(piContent);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_bar);
remoteViews.setTextViewText(R.id.textViewNotifPlaylist, "Sample Playlist");
remoteViews.setTextViewText(R.id.textViewNotifItem, "This is long text and I want to this at marquee mode");
remoteViews.setOnClickPendingIntent(R.id.imgBtnNotifPrevious, piPrevious);
remoteViews.setOnClickPendingIntent(R.id.imgBtnNotifPlayPause, piPausePlay);
remoteViews.setOnClickPendingIntent(R.id.imgBtnNotifNext, piNext);
builder.setContent(remoteViews);
notificationManager.notify(R.string.app_name, builder.build());
}
答案 0 :(得分:0)
Marquee需要textview获得焦点,因此您可以像这样重载textview:
public class MarqueeTextView extends TextView {
public MarqueeTextView(Context appCon) {
super(appCon);
}
public MarqueeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MarqueeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean isFocused() {
return true;
}
@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
}
}