0

プログラムでウィジェットの面を構成するにはどうすればよいですか?ウィジェットを作成しました:

レイアウト:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:layout_margin="2dip"
    android:background="@drawable/widget"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="3dip"
        android:layout_marginRight="3dip"
        android:layout_marginTop="3dip"
        tools:ignore="UseCompoundDrawables" >

        <TextView
            android:id="@+id/widget_title"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/app_name"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <ImageView
            android:id="@+id/edit_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dip"
            android:contentDescription="@string/edit"
            android:src="@drawable/widget_edit" />
    </LinearLayout>

    <TextView
        android:id="@+id/widget_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="4dip"
        android:layout_marginRight="4dip" />

</LinearLayout>

形状(@ drawable / widget):

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <stroke
        android:width="1dp"
        android:color="#FFFFFFFF" />

    <solid android:color="#00000000"/>

    <corners
        android:bottomLeftRadius="7dp"
        android:bottomRightRadius="7dp"
        android:topLeftRadius="7dp"
        android:topRightRadius="7dp" />

</shape>

構成アクティビティで、TextViewの背景色、背景の透明度、境界線の色、フォントサイズ/色を設定する必要があります。しかし、TextViewオブジェクトを取得する方法と、形状のプロパティにアクセスする方法は?

4

1 に答える 1

1

TextView通常のアクティビティとは異なり、ウィジェットでは、を使用して直接インスタンス化することはできませんfindViewById

RemoteViewsパッケージ名をパラメータとして渡して、オブジェクトをインスタンス化する必要があります。

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.my_widget);

そして、パラメータとしての元の関数を使用して、オブジェクトTextViewのメソッドを使用することに影響を与えます。setIntremoteViewsTextView

remoteViews.setInt(R.id.widget_title, "setBackgroundResource", android.R.color.white);

最後に、ウィジェットを更新します。

ComponentName thisWidget = new ComponentName(context, Widget.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(thisWidget, remoteViews);

public void setInt (int viewId, String methodName, int value)メソッドを使用して元の組み合わせをパラメーターとして渡すことで他の組み合わせを試すことができ methodNameますが、何らかの理由で、ウィジェットコンテキストですべてが許可されているわけではありません。やってみよう!

于 2013-03-12T14:35:05.290 に答える