0
0

More than 1 year has passed since last update.

Viewを突き出して表示させたい

Posted at

初めに

Viewを突き出して表示したい
Viewを他のViewと重ねて表示したい
そんな時のためにやり方を残しておきます

コードでxmlに書いていく

<FrameLayout 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"
tools:context=".MainActivity">

 <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="50dp"
    android:background="@color/teal_200">

    <FrameLayout
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:background="@color/purple_200" />
  </FrameLayout>
</FrameLayout>

スクリーンショット 2022-01-30 23.07.57.png

この様なLayoutを作っておきました
わかりやすい様に 突き出したい子ビューにパープルのカラー、親Viewに水色のカラーを付けておきました

このパープルの子Viewを上方向に突き出すために android:margin_top をマイナス方向に指定します

,,,

<FrameLayout
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="-20dp"
        android:background="@color/purple_200" />

スクリーンショット 2022-01-30 23.28.48.png

子Viewが上方向に半分食い込みました!w
でもこれではまだはみ出したViewが見切れていて困ってしまいますね

なので、はみ出した部分を表示させるために、親Viewの 更に親View(パープルの子Viewからみた祖父母View)に
android:clipChildren="false"
android:clipToPadding="false"
を加えます

<FrameLayout 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:clipChildren="false"
android:clipToPadding="false"
tools:context=".MainActivity">

スクリーンショット 2022-01-30 23.37.19.png

はみ出して表示させることができました!

最後に

今回はFrameLayoutを使って行いましたが、他の方法もあるみたいですので、機会があったら別の方法での記事も書いていきたいと思います

0
0
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
0
0