11

SQLite、独自の ContentProvider、およびローダーに関する Lars Vogel のチュートリアルでは、ToDo 項目のリストに次のレイアウトを使用しています ( http://www.vogella.com/articles/AndroidSQLite/article.html#todo_layouttodo_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> 

ここまでは順調ですね。それはうまく機能します。ImageViewAndroid 開発者ツール (Eclipse) は、 を のdrawable属性に置き換えることを提案していますTextView。次のレイアウト定義を試しました:

<?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正常に動作するようです。

ただし、drawable のサイズがわかるかどうかはわかりません。このImageViewソリューションでは、android:layout_width/height属性を使用して、必要なアイコンのサイズを伝えました。TextView-only ソリューションと に似たものはありandroid:drawable...ますか?

ありがとう、ペトル

4

1 に答える 1

14

TextView残念ながら、を使用して のドローアブル サイズを変更することはできませんxml。のみで行うことができます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);
于 2013-03-11T14:01:24.287 に答える