为什么我的小部件中的TextView中的文本颜色是白色的?

时间:2017-07-21 01:52:10

标签: android android-widget

目前我正在学习Android中的widget。以前我试图在我的虚拟项目中实现一个小部件,我可以用黑色显示TextView。但是,现在我有另一个项目,我用同样的方式实现了widget。我可以在小部件中显示文本,但问题是当我使用白色背景时文本的颜色是白色。我不认为我在之前的项目中修改了一些文本是黑色的。我在这里想念一些东西,比如传递错误的Context吗?

小部件项目:

<?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="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Body1" />

</LinearLayout>

RemoteViewsService:

public class WidgetItemService extends RemoteViewsService {

    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new ItemFactory();
    }

    class ItemFactory implements RemoteViewsService.RemoteViewsFactory {

        private List<String> mStrings;

        public IngredientItemFactory() {

        }

        @Override
        public void onCreate() {

        }

        @Override
        public void onDataSetChanged() {

            Cursor c = getContentResolver().query(StringContract.StringEntry.CONTENT_URI, null, null, null, null);
            mStrings = CursorUtils.extract(c);

        }

        @Override
        public void onDestroy() {

        }

        @Override
        public int getCount() {
            return mStrings.size();
        }

        @Override
        public RemoteViews getViewAt(int pos) {
            String string = mStrings.get(pos);

            RemoteViews views = new RemoteViews(getPackageName(), R.layout.widget_item);
            views.setTextViewText(R.id.tv_text, string);

            return views;
        }

        @Override
        public RemoteViews getLoadingView() {
            return null;
        }

        @Override
        public int getViewTypeCount() {
            return 1;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }
    }
}

的AppWidgetProvider:

public class Widget extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        for (int appWidgetId: appWidgetIds) {
            String string = PrefUtils.load(this, appWidgetId);

            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);

            Intent intent = new Intent(context, WidgetItemService.class);

            views.setRemoteAdapter(R.id.lv_list, intent);
            views.setEmptyView(R.id.lv__list, R.id.tv_empty);

            views.setTextViewText(R.id.tv_title, string);

            Intent openActivity = BActivity.createIntent(string);

            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, openActivity, PendingIntent.FLAG_UPDATE_CURRENT);

            views.setOnClickPendingIntent(R.id.widget, pendingIntent);

            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        for (int appWidgetId : appWidgetIds) {
            PrefUtils.delete(context, appWidgetId);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

根据你的代码。

<TextView
    android:id="@+id/tv_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="@style/Base.TextAppearance.AppCompat.Body1" />

我找到style="@style/Base.TextAppearance.AppCompat.Body1"

我点击了。就是这个。

<style name="Base.TextAppearance.AppCompat.Body1">
    <item name="android:textSize">@dimen/abc_text_size_body_1_material</item>
    <item name="android:textColor">?android:textColorPrimary</item>
</style>

所以你可以看看android:textColorPrimary

然后,您查看theme color