以前我們每個VC都有一個self.view,而Android也有個類似的東西☘️☘️☘️
所有的layout file一開始都會幫你預設一個ViewGroup,ViewGroup有很多種,而現在主要在推的ConstraintLayout就是其中一種。
不過它功能太多了,有點複雜,如果是簡單的畫面,用傳統的佈局方法也是很好用的。
如下簡介:
實作如下:
 
        LinearLayout linearLayout = findViewById(R.id.linear_layout);
        Button button = new Button(this);
        button.setText("new button by code");
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
        button.setLayoutParams(layoutParams);
        linearLayout.addView(button);
 
<Button
        android:id="@+id/relative_layout_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"/>
    
    //按鈕二在按鈕一的右邊
    <Button
        android:id="@+id/relative_layout_button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_toRightOf="@id/relative_layout_button"/>
    //按鈕三在按鈕二的下面,也在按鈕一的右邊
    //對齊父層下方
    <Button
        android:id="@+id/relative_layout_button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_below="@id/relative_layout_button2"
        android:layout_toRightOf="@id/relative_layout_button"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="10dp"/>
    //按鈕四在按鈕三的右邊,也在按鈕二的下面
    //對齊父層右方
    <Button
        android:id="@+id/relative_layout_button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_toRightOf="@id/relative_layout_button3"
        android:layout_below="@id/relative_layout_button2"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"/>
 
 
 
 
Android 三大布局的通用属性和各自的特有属性
UI實作範例(1):RelativeLayout, LinearLayout
可以去 https://github.com/mark33699/IDLA 看一下順便給顆⭐️
如果你喜歡我的影片別忘了按讚分享加訂閱,開啟紅色的小鈴鐺,我們明天見~