gpt4 book ai didi

安卓布局: on TextView and android:drawableStart -- setting size of the icon?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:05:24 26 4
gpt4 key购买 nike

Lars Vogel 的 SQLite 教程,自己的 ContentProvider 和 Loader 使用以下 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>

到目前为止,还不错。它工作得很好。 Android 开发人员工具 (Eclipse) 建议将 ImageView 替换为 TextViewdrawable 属性。我尝试了以下布局定义:

<?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 似乎工作正常。

但是,我不知道是否可以告诉 drawable 的大小。 ImageView 解决方案使用 android:layout_width/height 属性来告知所需的图标尺寸。 TextView-only 解决方案和 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);

关于安卓布局: on TextView and android:drawableStart -- setting size of the icon?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15340146/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com