2013-03-11 127 views
3

的拉爾斯沃格爾對的SQLite,ContentProvider的自己和裝載機教程使用以下佈局的待辦事項列表(檢查http://www.vogella.com/articles/AndroidSQLite/article.html#todo_layouttodo_row.xml佈局文件):Android佈局:在TextView和android上:drawableStart - 設置圖標的大小?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="30dp" 
     android:layout_height="24dp" 
     android:layout_marginLeft="4dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginTop="8dp" 
     android:src="@drawable/reminder" > 
    </ImageView> 

    <TextView 
     android:id="@+id/label" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="6dp" 
     android:lines="1" 
     android:text="@+id/TextView01" 
     android:textSize="24dp" 
     > 
    </TextView> 

</LinearLayout> 

到目前爲止,一切都很好。它很好地工作。 Android Developer Tools(Eclipse)建議用TextViewdrawable屬性替換ImageView。我嘗試了以下佈局定義:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:id="@+id/label" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="8dp" 
     android:layout_marginBottom="8dp" 

     android:layout_marginLeft="4dp" 
     android:drawablePadding="8dp" 
     android:drawableStart="@drawable/reminder"  

     android:lines="1" 
     android:text="@+id/TextView01" 
     android:textSize="24sp" 
     > 
    </TextView> 

</LinearLayout> 

I.e.使用drawableStart代替ImageView。相關android:layout_marginLeftandroid:drawablePadding似乎工作正常。

但是,我不知道是否可以告訴drawable的大小。 ImageView解決方案使用android:layout_width/height屬性來指示所需的圖標尺寸。有沒有類似的TextView - 只有解決方案和android:drawable...

感謝,切赫

回答

4

不幸的是,這是不可能的使用xml改變TextView的繪製大小。只能用Java完成。

final LinearLayout layout = <get or create layou here>; 
final TextView label = (TextView) layout.findViewById(R.id.label); 

final float density = getResources().getDisplayMetrics().density; 
final Drawable drawable = getResources().getDrawable(R.drawable.reminder); 

final int width = Math.round(30 * density); 
final int height = Math.round(24 * density); 

drawable.setBounds(0, 0, width, height); 
label.setCompoundDrawables(drawable, null, null, null); 
+1

非常感謝弗拉基米爾(?)。 – pepr 2013-03-11 14:08:32