2012-02-06 45 views
0

我創建了一個帶有圖標的Android小部件,該圖標在右上角顯示一個數字,表示一個小小的白色圓圈。現在,如果我的價值是一個角色,那麼價值就在小白衣圈子中間。但是當這個值是2個字符時,它就不再適合了。Android小部件在運行時更改邊距

試圖做的是,如果我的值超過1個字符,那麼我想更改textview的左邊距,使得值位於圖標一部分的白色圓圈的中心。

我嘗試了所有準備與

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

但是,給出了一個例外。 有沒有辦法在運行時改變widget的左邊距?

我的佈局文件的樣子:

<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

添加到您的TextView在XML

android:gravity="center_vertical|center_horizontal" 

您的文字永遠是中心。

+0

我添加了佈局文件。 我使用的是有一個裕度左側58傾角。如果我的文本是2個字符或更多,我想將我的填充更改爲像56 dip或les這樣的文本,因此文本更多地移動到左側。 – 2012-02-07 07:55:18

0

我找到了解決方案:)這確實是一個向文本框中添加grafity:center的佇列。

我改變了我的佈局如下:

<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對齊圖標的右側,寬度尺寸與小圓圈一樣。 a grafity:中心運作良好。