ViewGroup.MarginLayoutParams.layout_marginTop

とあるViewから見て、同じ階層にある近くの他のViewとの間隔を決める場合や、親のViewの境界線との間隔を決める場合、layout_margin~を使う・・・。

ViewGroup.MarginLayoutParams | Android Developers

今回はlayout_marginTopで、上との間隔だけセットする・・・。

strings.xmlで、

このようにして、

最初は、

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#f00"
        android:text="@string/textview1_text" />
            
</LinearLayout>

layout_marginTopを設定せずに赤い背景のTextViewを置いてみる・・・。

f:id:BG1:20170102144131p:plain

マージンがないので、左上にぴったりくっついている・・・。

そこで、

今度は、layout_marginTopに100dpをセットすると、

f:id:BG1:20170102144320p:plain

このように上との間隔がだいぶ空く・・・。
この間隔がちょうど100dpになっているのである・・・。
このように、layout_margin~を使って、Viewの間隔や位置を調整できる・・・。

Sample/android/ViewGroup.MarginLayoutParams/layout_marginTop/src/ViewGroup.MarginLayoutParams at master · bg1bgst333/Sample · GitHub