Android的布局:在TextView的和android:drawableStart - 图标的设置大小? [英] Android layout: on TextView and android:drawableStart -- setting size of the icon?

查看:2101
本文介绍了Android的布局:在TextView的和android:drawableStart - 图标的设置大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在拉斯沃格尔上的的SQLite,ContentProvider的自己和装载机的使用为待办事项列表中的以下布局(检查的 http://www.vogella.com/articles/AndroidSQLite/article.html#todo_layout todo_row.xml 布局文件):

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):

<?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开发工具(Eclipse的)建议,以取代的ImageView 绘制的属性的TextView 。我尝试了以下布局定义:

So far, so good. It works nicely. The Android Developer Tools (Eclipse) suggests to replace the ImageView by the drawable attribute of the TextView. 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>

即。在 drawableStart 代替了的ImageView 。相关的android:layout_marginLeft 的android:drawablePadding 似乎很好地工作。

I.e. the drawableStart was used instead of the ImageView. The related android:layout_marginLeft and android:drawablePadding seems to work fine.

不过,我不知道是否有可能告诉绘制的大小。在的ImageView 解决方案使用了的android:layout_width / 高度属性告诉想要的图标尺寸。有什么为的TextView - 只有解决方案和 Android的类似:绘制...

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. Is there anything similar for the TextView-only solution and the android:drawable...?

谢谢,切赫

推荐答案

不幸的是,这是不可能的使用改变的的TextView 绘制尺寸 XML 。它可以做到的Java

Unfortunately, it's not possible to change the TextView's drawable size using xml. It can be done with Java only.

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);

这篇关于Android的布局:在TextView的和android:drawableStart - 图标的设置大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆