简体   繁体   English

Android布局:在TextView和android:drawableStart上 - 设置图标的大小?

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

The Lars Vogel's tutorial on SQLite, own ContentProvider and Loader uses the following layout for the list of ToDo items (check the http://www.vogella.com/articles/AndroidSQLite/article.html#todo_layout , todo_row.xml layout file): 在拉斯沃格尔对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> 

So far, so good. 到现在为止还挺好。 It works nicely. 它工作得很好。 The Android Developer Tools (Eclipse) suggests to replace the ImageView by the drawable attribute of the TextView . Android Developer Tools(Eclipse)建议用TextViewdrawable属性替换ImageView I tried the following layout definition: 我尝试了以下布局定义:

<?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>

Ie the drawableStart was used instead of the ImageView . 即使用drawableStart而不是ImageView The related android:layout_marginLeft and android:drawablePadding seems to work fine. 相关的android:layout_marginLeftandroid:drawablePadding似乎工作正常。

However, I do not know if it is possible to tell the size of the drawable. 但是,我不知道是否可以分辨出可绘制的大小。 The ImageView solution used the android:layout_width / height attributes to tell the wanted icon dimensions. ImageView解决方案使用android:layout_width / height属性来告诉想要的图标尺寸。 Is there anything similar for the TextView -only solution and the android:drawable... ? TextView -only解决方案和android:drawable...有什么类似的东西android:drawable...

Thanks, Petr 谢谢,彼得

Unfortunately, it's not possible to change the TextView 's drawable size using xml . 不幸的是,使用xml无法更改TextView的可绘制大小。 It can be done with Java only. 它只能用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