2012-02-06 11 views
0

右上隅の数字を小さな白い円で示すアイコンでアンドロイドウィジェットを作成しました。今私の値が1文字の場合、その値はちょっとした白い円の真ん中です。しかし値が2文字のときは、もはや真ん中にはありません。実行時のAndroidウィジェットの変更マージン

私の値が1文字以上の場合、値がアイコンの一部である白い円の中心になるように、テキストビューの左余白を変更したいとします。

私はすべての

RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget); 
updateViews.setInt(R.id.Value, "setMargins", 10); 

と準備しようとしたしかし、それは例外を提供します。 実行時にウィジェットの左余白を変更する方法はありますか?

マイレイアウトファイルには、次のようになります。

<ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/widget_alarm" 
     android:id="@+id/imageAlarm" 
     android:layout_marginLeft="10dp" 
     android:layout_centerVertical="true" 
    /> 

    <TextView 
     android:id="@+id/alarmCount" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="22dip" 
     android:layout_marginLeft="58dip" 
     android:text="0" 
     android:textColor="#000000" 
    /> 

答えて

0

はあなたのテキストは常に中央になります

XMLであなたのTextViewに

android:gravity="center_vertical|center_horizontal" 
を追加します。

+0

レイアウトファイルを追加しました。 これは、余白が58ディップのままになっているものを使用します。私のテキストが2文字以上の場合、私はパッディングを56 dipやlesのように変えて、テキストが左に移動するようにしたい。 –

0

私は解決策を見つけました:)実際には、テキストボックスに勾配:中心を追加するという決定でした。

私を次のように私のレイアウトを変更:

<ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/widget_alarm" 
     android:id="@+id/imageAlarm" 
     android:layout_marginLeft="10dp" 
     android:layout_centerVertical="true" 
    /> 

    <TextView 
     android:id="@+id/alarmCount" 
     android:layout_width="25dp" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="22dip" 
     android:gravity="center" 
     android:textSize="12dip" 
     android:text="000" 
     android:textColor="#d21925" 
     android:layout_alignRight="@+id/imageAlarm" 
    /> 

今のTextViewはアイコンの右側に整列され、幅が少し丸い円と同じサイズです。 grafity:centerはうまくいきます。

関連する問題