Androidのレイアウトを編集していると、
This tag and its children can be replaced by one <TextView/> and a compound drawable
という警告が出ることがあります。
これはImageViewとTextViewを並べて配置したときに、それはひとまとめにできるからTextViewとCompound drawableを使いなさいよというメッセージです。
実際、ひとまとめにできるなら良いのですが、ImageViewの縮小機能を使いたいなど、別にしておく必要があることが多々あります。
というわけで、警告を無視する設定を追加します。
レイアウトXMLのルート要素に
xmlns:tools="http://schemas.android.com/tools"
があることを確認します。これはtools namespaceの定義です。古いプロジェクトにはないことが多いので、なければ追加してください。
で、ImageViewとTextViewを覆うLinearLayoutに
tools:ignore="UseCompoundDrawables"
属性を追加します。
全体はこんな感じです。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical"
tools:ignore="UseCompoundDrawables" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="@null"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/app_name"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
tools:ignore属性をつけると、Lintチェックをその要素だけ回避できます。ルート要素に付ければ、そのXML全体で効きます。
警告メッセージからのLint名は、以下のLint一覧から探してください。
Android Lint Checks – Android Tools Project Site
タグ: Layout

HOMMA Teppei

