gpt4 book ai didi

android - StackedWidget View 未显示

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:04:39 25 4
gpt4 key购买 nike

当我的 StackedWidget View 只是一个 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;
}

最佳答案

如果您使用的是本地镜像,请使用 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;
}

ImageView 中移除 clickableonClick 属性。 onClick 属性导致添加 setOnClickListener。可以引用这个answer我之前写过。如果你想在远程 View 的情况下处理 ImageView 上的点击事件,你应该使用 RemoteViews.setOnClickPendingIntent .

关于android - StackedWidget View 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23739730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com