【发布时间】:2014-07-07 12:43:51
【问题描述】:
当我的 StackedWidget 视图只是一个 textview 时,它会显示在启动器屏幕上,但布局更复杂(A RelativeLayout、imageview、textviews)它不会显示任何内容
关于设置 RemoteViews 有什么我需要知道的吗?
widget_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_item"
android:layout_width="270dp"
android:layout_height="150dp">
<ImageView
android:id="@+id/timelineCellImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:onClick="onClick"
android:scaleType="centerCrop"
android:src="@drawable/bottom_grey_gradient"
/>
<LinearLayout
android:id="@+id/timelineCellTextHolder"
android:layout_width="match_parent"
android:layout_height="@dimen/fifty_dp"
android:layout_alignBottom="@id/timelineCellImage"
android:layout_alignLeft="@id/timelineCellImage"
android:background="@drawable/rounded_edges_bottom_dark"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginLeft="@dimen/five_dp"
android:layout_marginRight="@dimen/five_dp"
android:layout_marginTop="@dimen/ten_dp"
android:layout_weight=".5"
android:ellipsize="end"
android:gravity="bottom"
android:lines="1"
android:text="Test"
android:textColor="@color/white"
android:textSize="@dimen/twelve_sp"
android:textStyle="bold" />
<TextView
android:id="@+id/subheading"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginBottom="@dimen/ten_dp"
android:layout_marginLeft="@dimen/five_dp"
android:layout_marginRight="@dimen/five_dp"
android:layout_weight=".5"
android:ellipsize="end"
android:gravity="top"
android:lines="1"
android:text="Subtest"
android:textColor="@color/white"
android:textSize="@dimen/twelve_sp" />
</LinearLayout>
StackWidgetService.java $getViewAt(int)
public RemoteViews getViewAt(int position) {
// position will always range from 0 to getCount() - 1.
RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
if(!mWidgetItems.isEmpty()){
// We construct a remote views item based on our widget item xml file, and set the
// text based on the position.
rv.setTextViewText(R.id.title, mWidgetItems.get(position).getTitle()); //probably only top stories items, editorial stuff
rv.setTextViewText(R.id.subheading, mWidgetItems.get(position).getLinkAbstract());
rv.setImageViewUri(R.id.timelineCellImage, Uri.parse(mWidgetItems.get(position).getCoverImageUrl()));
// Next, we set a fill-intent which will be used to fill-in the pending intent template
// which is set on the collection view in StackWidgetProvider.
Bundle extras = new Bundle();
extras.putInt(StackWidgetProvider.EXTRA_ITEM, position);
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
rv.setOnClickFillInIntent(R.id.widget_item, fillInIntent);
// You can do heaving lifting in here, synchronously. For example, if you need to
// process an image, fetch something from the network, etc., it is ok to do it here,
// synchronously. A loading view will show up in lieu of the actual contents in the
// interim.
/*new ImageRequest(
url,
listener,
maxWidth,
maxHeight,
decodeConfig,
errorListener);
Response.Listener<Bitmap> listener = new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap bitmap) {
// use your bitmap
}
};*/
}
else
{
rv.setTextViewText(R.id.title, "my title");
rv.setTextViewText(R.id.subheading, "");
rv.setImageViewResource(R.id.timelineCellImage, R.drawable.top_grey_gradient);
Bundle extras = new Bundle();
extras.putInt(StackWidgetProvider.EXTRA_ITEM, position);
Intent fillInIntent = new Intent();
fillInIntent.putExtras(extras);
rv.setOnClickFillInIntent(R.id.widget_item, fillInIntent);
}
try {
System.out.println("Loading view " + position);
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Return the remote views object.
return rv;
}
【问题讨论】:
-
分享xml布局和用于将膨胀布局添加到RemoteView的代码。
-
@ManishMulimani 好的,已添加。
-
getCoverImageUrl是否返回内容 URI?setImageViewUri适用于内容 URI 而不是网络资源。如果您使用网络资源,则需要设置位图。还要从布局文件中删除 ImageView 的onClick和clickable属性。
标签: android android-widget remoteview