android layout:这个标签及其子代可以替换为一个<TextView />和一个复合drawable

pdi*_*lip 107 layout android

当我在特定的XML文件上运行布局时,我得到了这个:

 This tag and its children can be replaced by one <TextView/> 
and a compound drawable
Run Code Online (Sandbox Code Playgroud)

应对以下xml代码进行哪些更改:

<LinearLayout android:id="@+id/name_layout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:background="@drawable/grouplist_single_left_grey_area" >
                <ImageView android:id="@+id/photo_image"
                    android:layout_width="@dimen/thumbnail_width"
                    android:layout_height="@dimen/thumbnail_height"
                    android:paddingBottom="5dip"
                    android:paddingTop="5dip"
                    android:paddingRight="5dip"
                    android:paddingLeft="5dip"
                    android:layout_marginRight="5dip"
                    android:clickable="true"
                    android:focusable="true"
                    android:scaleType="fitCenter"
                    android:src="@*android:drawable/nopicture_thumbnail"
                    android:background="@drawable/photo_highlight" />
                <TextView android:id="@+id/name"
                    android:paddingLeft="5dip"
                    android:layout_weight="1"
                    android:layout_width="0dip"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:textAppearance="?android:attr/textAppearanceLarge" />
            </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

这就是它在屏幕上的样子:

在此输入图像描述

相机图标是默认设置.单击该按钮将为用户提供选择其他图像的选项.

NPi*_*ike 144

为了扩展Romain Guy的答案,这是一个例子.

之前:

<LinearLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
android:layout_marginTop="10dp"  
android:padding="5dp" >

<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="My Compound Button" />

<ImageView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/my_drawable" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

后:

<TextView  
    android:layout_marginTop="10dp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="My Compound Button" 
    android:drawableRight="@drawable/my_drawable" android:padding="5dp" />
Run Code Online (Sandbox Code Playgroud)

  • 俏皮.但是如果我需要在drawable上设置属性,比如将它定义为60dip x 60dip呢? (44认同)
  • 嘿,看看,我所要做的只是搜索.http://stackoverflow.com/a/6671544/1224741 (4认同)

Rom*_*Guy 101

通过使用TextView的方法或使用将TextView其合并ImageView为一个.setCompoundDrawable*()android:drawableLeft

  • 你能指点我一个例子说明如何做到这一点吗?我不确定ImageView中的所有属性如何适应android:drawableLeft.谢谢 (30认同)
  • 如何增加图像和文本之间的差距? (2认同)
  • setCompoundDrawable*()无效,请改用setCompoundDrawablesWithIntrinsicBounds() (2认同)

QED*_*QED 18

以为我会尝试为此添加一些额外的puntos:您可以使用在图像和文本之间添加填充android:drawablePadding./sf/answers/467008111/