LoginSignup
73
70

More than 5 years have passed since last update.

xmlns:toolsでAndroid Studioのレイアウトエディタを活用する

Last updated at Posted at 2014-08-21

xmlns:tools?

Androidのレイアウトファイルに以下のような記述が含まれていることがあります。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MyActivity"

このxmlns:toolstools:contextとはなんでしょうか。

答えは、Android Tools Projectに書いてありました。
http://tools.android.com/tech-docs/tools-attributes

英語よくわからん

という僕自身のために、内容を簡単にまとめました。

tools:ignore

lint用
tools:ignoreはリソースのすべてのxml要素に定義できる。
これを定義したxml要素とその子孫要素は、lintによるチェックで指定したissueが無視されるようになる。
複数のissueを無視したい場合、カンマ区切りで指定する。

<string name="show_all_apps" tools:ignore="MissingTranslation">All</string>

tools:targetApi

lint用
javaの@TargetApiと同じようなものでlintによるチェック時に適用される。
ApiLevelの指定はコードネームと整数両方いけるらしい。

<GridLayout tools:targetApi="ICE_CREAM_SANDWICH" >

tools:locale

lint/AndroidStudio用
values系リソースファイルのルート要素に定義できる。
これを指定したリソースの文字列は指定ロケール扱いになるらしい。
…デフォルトvaluesの文字列リソースが英語じゃない場合に使うと思うのだけど正直わからん。
結局日本語アプリではどうするべきなの?

<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="es">

tools:context

lint/AndroidStudio/Eclipse用
レイアウトxmlのルート要素で定義する。
レイアウトエディタでの表示に指定Activityのテーマが適用されるようになる。
Activityの指定はAndroidManifest同様packageからの相対パスで指定できるらしい。

<android.support.v7.widget.GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity"

tools:layout

AndroidStudio/Eclipse用
レイアウトxmlのfragment要素で定義する。
レイアウトエディタでのfragment要素の表示に指定したレイアウトが適用されるようになる。

<fragment android:name="com.example.master.ItemListFragment" tools:layout="@android:layout/list_content" />

tools:listitem / listheader / listfooter

AdapterView の子要素(ListViewなど)に定義できる。
それぞれ子要素のレイアウト、ヘッダのレイアウト、フッタのレイアウトを指定する。
レイアウトエディタでのAdapterViewの表示時に指定したレイアウトが適用されるようになる。

 <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@android:layout/simple_list_item_2" />

tools:showIn

AndroidStudio(0.5.8以降)用。
他のレイアウトから<include>されるレイアウトのルート要素に定義できる。
AndroidStudioでレイアウトエディタ表示時に親レイアウトに埋め込まれた状態で表示されるらしい。
http://tools.android.com/recent/androidstudio058released

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:showIn="@layout/activity_main" />

tools:menu

AndroidStudio(0.8.0以降)用。
レイアウトのルート要素に定義できる。
AndroidStudioのレイアウトエディタではtools:contextActivityが指定されている場合、
Activity#onCreateOptionsMenuまでみてメニューアイテムを表示するらしい。
この要素が定義されているとそれを無視して指定したメニューアイテムを描画するようだ。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:menu="menu1,menu2" />

tools:actionBarNavMode

AndroidStudio(0.8.0以降)用。
レイアウトのルート要素に定義できる。
この要素が定義されている場合、レイアウトエディタにおいてActionBarを指定したモードで描画することができる。
"standard", "list", "tabs"のいずれかを指定する。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:actionBarNavMode="tabs" />

まとめ

toolsについて調べていたつもりがAndroidStudioのレイアウトエディタの進化に驚かされました。
レイアウトエディタからキャプチャも作れるので、tools属性を駆使すれば実機で確認ステップを少し減らせそうですね。
ぜひ活用していきたいです。

73
70
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
73
70