[英]Android layout: on TextView and android:drawableStart — setting size of the icon?
在拉斯沃格爾對SQLite的教程,自己的ContentProvider和Loader使用用於待辦事項列表如下布局(檢查http://www.vogella.com/articles/AndroidSQLite/article.html#todo_layout , todo_row.xml
布局文件) :
<?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)建議用TextView
的drawable
屬性替換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>
即使用drawableStart
而不是ImageView
。 相關的android:layout_marginLeft
和android:drawablePadding
似乎工作正常。
但是,我不知道是否可以分辨出可繪制的大小。 ImageView
解決方案使用android:layout_width
/ height
屬性來告訴想要的圖標尺寸。 TextView
-only解決方案和android:drawable...
有什么類似的東西android:drawable...
?
謝謝,彼得
不幸的是,使用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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.