我有一个带有TextView的app小部件。
<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(...)工作正常。
我不想创建另一个TextViews或Layouts。还有其他改变重力的方法吗?
答案 0 :(得分:1)
我的解决方案是:
// Text Align
int gravity = widget_pf.getInt(Constants.Prefs.WIDGET_TEXT_ALIGN, 2);
if (gravity == 0)
remoteViews.setInt(R.id.BalancesLinLayout, "setGravity", Gravity.LEFT);
else if (gravity == 1)
remoteViews.setInt(R.id.BalancesLinLayout, "setGravity", Gravity.CENTER_HORIZONTAL);
else
remoteViews.setInt(R.id.BalancesLinLayout, "setGravity", Gravity.RIGHT);
在widget_layout.xml中:
<LinearLayout
android:id="@+id/BalancesLinLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="@+id/SampleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:textColor="#FFFFFF"
android:textSize="9sp" />
</LinearLayout>
到目前为止,这很简单。
答案 1 :(得分:0)
所以......这是我的解决方案。 小部件布局:
<?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);
P.S。我知道,这是一种糟糕的编码风格,但互联网上的其他变体看起来是相同的=(