Androidのwrapcontentとfillparentの例
Androidでは、コンポーネントの属性 “layout
width”と “layout
height”に常に “` wrap
content`または
` fill
parent “を入れます。何が違うのでしょうか?
次の定義を参照してください。
-
wrap__content
– コンポーネントは、
そのコンテンツのみを囲みます。
-
fill__parent
– コンポーネントが親コンポーネントと同じ大きさに表示されたい場合、
残りのスペースを記入してください。 (APIレベル8のmatch__parentの名前が変更されました)
上記の用語は意味をなさないかもしれませんが、以下のデモンストレーションを見てください:
1. wrap__content
ボタンコンポーネントは、width属性とheight属性の両方に “wrap
content”を設定します。それはAndroidのコンテンツに ”
Button ABC__”のみを囲むのに十分な大きさのボタンを表示するように指示します。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout__width="fill__parent" android:layout__height="fill__parent" > <Button android:id="@+id/btnButton1" android:layout__width="wrap__content" android:layout__height="wrap__content" android:text="Button ABC"/> </RelativeLayout>
2. fill__parent – 幅
layout
width
を
fillparent
に変更すると、ボタンの幅は親の “RelativeLayout`”と同じ大きさの残っているスペースを埋めるでしょうが、ボタンの高さはまだそれを囲むだけの大きさです。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout__width="fill__parent" android:layout__height="fill__parent" > <Button android:id="@+id/btnButton1" android:layout__width="fill__parent" android:layout__height="wrap__content" android:text="Button ABC"/> </RelativeLayout>
3. fill__parent – 高さ
“layout
height”を “fill
parent”に変更すると、ボタンの高さが親の “RelativeLayout”と同じ大きさの残りのスペースに塗りつぶされますが、ボタンの幅はまだ内容を囲むだけの大きさです。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout__width="fill__parent" android:layout__height="fill__parent" > <Button android:id="@+id/btnButton1" android:layout__width="wrap__content" android:layout__height="fill__parent" android:text="Button ABC"/> </RelativeLayout>
4. fill__parent – 幅、高さ
“layout
width”と “layout
height”を “fill__parent”に変更すると、ボタンはデバイス全体の画面と同じくらい大きく表示され、スクリーンスペース全体に表示されます。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout__width="fill__parent" android:layout__height="fill__parent" > <Button android:id="@+id/btnButton1" android:layout__width="fill__parent" android:layout__height="fill__parent" android:text="Button ABC"/> </RelativeLayout>
参考文献
XMLレイアウトドキュメント]。
http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html
[Android
ViewGroup.LayoutParamsドキュメント]
リンク://タグ/アンドロイド/[アンドロイド]