gpt4 book ai didi

android - RemoteViews + TextView + setGravity

转载 作者:太空狗 更新时间:2023-10-29 14:30:00 25 4
gpt4 key购买 nike

我有一个带有 TextView 的应用小部件。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/widgetText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/defaultWidgetText"
android:gravity="center"
/>
</FrameLayout>

我正在尝试通过调用

来更改文本对齐方式
views.setInt(R.id.widgetText, "setGravity", Gravity.CENTER);

来自 RemoteView。但它使“问题加载小部件”其他 setInt(...) 工作正常。

我不想创建另一个 TextView 或布局。还有其他方法可以改变重力吗?

最佳答案

所以...这是我的解决方案。小部件布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/top_left_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="invisible"
android:gravity="top|left"
/>
<TextView
android:id="@+id/top_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="invisible"
android:gravity="top|center_horizontal"
/>
<TextView
android:id="@+id/top_right_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="invisible"
android:gravity="top|right"
/>

<TextView
android:id="@+id/left_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="invisible"
android:gravity="left|center_vertical"
/>
<TextView
android:id="@+id/center_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="visible"
android:gravity="center"
/>
<TextView
android:id="@+id/right_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="invisible"
android:gravity="right|center_vertical"
/>

<TextView
android:id="@+id/bottom_left_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="invisible"
android:gravity="bottom|left"
/>
<TextView
android:id="@+id/bottom_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="invisible"
android:gravity="bottom|center_horizontal"
/>
<TextView
android:id="@+id/bottom_right_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="invisible"
android:gravity="bottom|right"
/>
</FrameLayout>

和 onUpdate(...) 方法:

widgetViews = new Hashtable<Integer, Integer>();

widgetViews.put(GRAVITY_TOP_LEFT, R.id.top_left_text);
widgetViews.put(GRAVITY_TOP, R.id.top_text);
widgetViews.put(GRAVITY_TOP_RIGHT, R.id.top_right_text);

widgetViews.put(GRAVITY_LEFT, R.id.left_text);
widgetViews.put(GRAVITY_CENTER, R.id.center_text);
widgetViews.put(GRAVITY_RIGHT, R.id.right_text);

widgetViews.put(GRAVITY_BOTTOM_LEFT, R.id.bottom_left_text);
widgetViews.put(GRAVITY_BOTTOM, R.id.bottom_text);
widgetViews.put(GRAVITY_BOTTOM_RIGHT, R.id.bottom_right_text);

Intent intent = new Intent(context, MinimalisticNotesWidgetActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
for(int id : widgetViews.values())
{
views.setViewVisibility(id, View.INVISIBLE);
}

int id = widgetViews.get(textGravity);

views.setOnClickPendingIntent(id, pendingIntent);
views.setTextViewText(id, widgetText);
views.setFloat(id, "setTextSize", fontSize);
views.setInt(id, "setTextColor", textColor);
views.setViewVisibility(id, View.VISIBLE);

附言我知道,这是一种糟糕的编码风格,但互联网上的其他变体看起来都一样 = (

关于android - RemoteViews + TextView + setGravity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7825508/

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