【问题标题】:Android layout: on TextView and android:drawableStart -- setting size of the icon?Android 布局:在 TextView 和 android:drawableStart 上——设置图标的大小?
【发布时间】:2013-02-26 17:19:12
【问题描述】:

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... 有什么相似之处吗?

谢谢,彼得

【问题讨论】:

    标签: android android-layout android-widget textview


    【解决方案1】:

    很遗憾,无法使用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);
    

    【讨论】:

      猜你喜欢
      • 2023-03-06
      • 1970-01-01
      • 2012-11-17
      • 2012-01-12
      • 2018-08-16
      • 1970-01-01
      • 2015-04-30
      • 2023-03-07
      • 2022-08-11
      相关资源
      最近更新 更多