【问题标题】:This tag and its children can be replaced by one <TextView/> and a compound drawable: picture and text这个标签及其子标签可以被一个 <TextView/> 和一个复合可绘制对象替换:图片和文本
【发布时间】:2014-03-28 00:40:51
【问题描述】:

我有这个代码

<ImageView
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_gravity="center_vertical"
                        android:background="@drawable/tick"
                        android:contentDescription="img" />

                    <TextView
                        android:id="@+id/txt1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:text="text1 "
                        android:textColor="#fff"
                        android:textSize="@dimen/sizeNormal" />

我收到警告This tag and its children can be replaced by one &lt;TextView/&gt; and a compound drawable

如何将它们合并到一个文本视图中?

我已经尝试过,但同时我遇到了刻度图像尺寸问题。

这里可以看到图片

【问题讨论】:

标签: android


【解决方案1】:

只是一个警告,应用程序仍应编译和运行。但是,这并不意味着您只想忽略所有警告。

现在,有不同的方法可以做到这一点。您可以在 xml 中使用 drawableLeft。类似的东西

<TextView
      android:id="@+id/txt1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="10dp"
      android:text="text1 "
      android:textColor="#fff"
      android:textSize="@dimen/sizeNormal"
      android:drawableLeft="@drawable/tick />

您还可以查看 here in the docs 在您的 Java 代码中调整它的不同方式。

要在图像和文本之间设置padding,请尝试使用

yourTextView.setCompoundDrawablePadding(somePadding);  

其中somePaddingint,表示您想要的padding 的值。

【讨论】:

  • 还需要确定图像布局尺寸
  • 看起来你需要缩小图像
  • 我现在更改了 img 的大小,我将如何将文本稍微向右移动
  • 看看我链接的那些方法。我相信他们都是你来添加填充的。在我的手机上,但如果你不明白,也许我可以看一下
  • 我需要在 img 和 text 之间放一个空格
【解决方案2】:

使用文本Compound Drawables。

<TextView
    android:id="@+id/txtTitle"
    android:drawableLeft="@drawable/g_device_access_storage"
    android:drawableRight="@drawable/g_navigation_refresh"
    android:paddingRight="55dp"
    android:text="hello" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-24
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 2011-10-28
    相关资源
    最近更新 更多