忍者ブログ
  • 2025.01
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 2025.03
[PR]
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

【2025/02/23 14:45 】 |
FrameLayout
FrameLayout
1つのオブジェクトだけを配置するレイアウト。
基本的に左上に置くだけ。複数の子オブジェクトを配置したら単に重なる。ただしlayout_gravity指定は可能。
子オブジェクトとしてLinearLayoutやRelativeLayoutを置くことも可能。
子オブジェクトの表示・非表示を切り替えて、表示内容を変えたりに使うようだ。

RelativeLayoutでも同じことできるじゃんとは思う。ただ意味的にFrameLayoutが合っているということか。


要素

foreground
このレイアウト全体を覆う画像。最前面に表示される。idで指定。
#rgb, #aarrggbb などで色指定も可能。ただしこの場合、後述のforegroundGravityと併用はできない(色がつかなくなる)




<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.layoutmaster.MainActivity" >

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="86dp"
android:layout_marginTop="45dp"
android:foreground="@drawable/abc_ic_search" > <!-- ここ!!!!!!! ->

<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="TextView" />
</FrameLayout>

</RelativeLayout>







foregroundにsearch(虫眼鏡)画像。
子ビューにドロイド君画像。その手前の子ビューにTextView。
重なりとしては
(最前面)虫眼鏡→TextView→ドロイド(最背面)


foregravityGroundを指定すると、foreground画像は本来のサイズになり、指定方向に寄る


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.layoutmaster.MainActivity" >

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="86dp"
android:layout_marginTop="45dp"
android:foreground="@drawable/abc_ic_search"
android:foregroundGravity="right"
>

<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="TextView" />
</FrameLayout>
</RelativeLayout>








なお、子オブジェクトがgoneになると、FrameLayoutのサイズ自体が縮まる

↓これが…

↓こうなる。


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.layoutmaster.MainActivity" >

<FrameLayout
android:id="@+id/frameLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="86dp"
android:layout_marginTop="45dp"
android:foreground="@drawable/abc_ic_search"
android:foregroundGravity="right" >

<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" <!-- 注目!!!!!! ->
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="24sp" />

</FrameLayout>

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/frameLayout1"
android:layout_below="@+id/frameLayout1"
android:src="@drawable/abc_ab_bottom_solid_dark_holo" />

</RelativeLayout>







ただし、measureAllChildrenをtrueにしておくと、可視状態をgoneにしても約まらない。


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.layoutmaster.MainActivity" >

<FrameLayout
android:id="@+id/frameLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="86dp"
android:layout_marginTop="45dp"
android:foreground="@drawable/abc_ic_search"
android:foregroundGravity="right"
android:measureAllChildren="true" > <!-- 注目!!!!!! ->

<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textSize="24sp" />

</FrameLayout>

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/frameLayout1"
android:layout_below="@+id/frameLayout1"
android:src="@drawable/abc_ab_bottom_solid_dark_holo" />

</RelativeLayout>







参考…可視状態
VISIBLE 見える
INVISIBLE 見えない。非表示部分を詰めない
GONE 見えない。非表示部分を詰める

PR
【2014/08/29 12:33 】 | Android | 有り難いご意見(0)
<<TableLayout | ホーム | RelativeLayout>>
有り難いご意見
貴重なご意見の投稿














<<前ページ | ホーム | 次ページ>>