簡體   English   中英

Android布局:在TextView和android:drawableStart上 - 設置圖標的大小?

[英]Android layout: on TextView and android:drawableStart — setting size of the icon?

在拉斯沃格爾對SQLite的教程,自己的ContentProvider和Loader使用用於待辦事項列表如下布局(檢查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> 

到現在為止還挺好。 它工作得很好。 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>

即使用drawableStart而不是ImageView 相關的android:layout_marginLeftandroid: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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM