当我的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;
}
答案 0 :(得分:4)
如果您使用的是本地图片,请使用RemoteViews.setImageViewUri
。
String url = new File(mWidgetItems.get(position).getCoverImageUrl()).toString();
rv.setImageViewUri(R.id.timelineCellImage, Uri.parse(url));
如果您使用的是网络资源,请使用RemoteViews.setImageViewBitmap
。
Bitmap bm = getImageBitmap(mWidgetItems.get(position).getCoverImageUrl());
rv.setImageViewBitmap(R.id.timelineCellImage, bm);
// Helper method to get Image bitmap
private Bitmap getImageBitmap(String purl) {
Bitmap bm = null;
try {
URL url = new URL(purl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
try {
InputStream in = new BufferedInputStream( urlConnection.getInputStream() );
bm = BitmapFactory.decodeStream(in);
} finally {
urlConnection.disconnect();
}
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bm;
}
从clickable
删除onClick
和ImageView
个属性。 onClick
属性会导致添加setOnClickListener
。你可以参考我之前写的answer。如果您想在远程观看的情况下处理ImageView
上的点击事件,您应该使用RemoteViews.setOnClickPendingIntent。