A list,

  • Android common layout unit (DP, SP, PX, PT, in, mm)

  • Layout parameters are used to set the position and size of the view in the layout.

Second,xmlcodewriting

  • Layout_xxx Layout properties:

    <TextView
        android:layout_width="100dp"
        android:layout_height="200dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="10dp"
        android:text="Hello world!"
        android:textSize="10sp" />
    Copy the code
  • The ViewGroup class implements a nested class that extends ViewGroup.layoutParams and contains properties that set the size and position of the view.

    TextView TV = new TextView(this); LinearLayout = new LinearLayout(this); LinearLayout = new LinearLayout(this); / / get in the view layout parameter object LinearLayout. LayoutParams params = (LinearLayout. LayoutParams) TV. GetLayoutParams (); // Configure the layout property params.leftMargin = 30; // left margin params.topMargin = 30; // top margin params.width = 100; // width params.height = 200; // high // Set the configured layout parameters to the view tv.setLayoutParams(params); // Add the view module to the layout module layout.addView(TV);Copy the code

Three, attributes,

  • In general, it is recommended not to use absolute units (e.g., pixel px) to specify layout width and height. It is better to use relative units of measurement (e.g., density-independent pixel units dp, WRAP_content, match_parent) to ensure that applications display correctly on screens of all sizes.

  • Wrap_content: The view resizes it to the size the content needs.

  • Match_parent: The view is as large as possible as its parent view group allows.

Four cases,

  • XML layout code

    
            
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <! The outermost layer is not Layout, so this is root Layout.
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffc">
            <! Add an internal view layout -->
            <LinearLayout
                android:layout_marginTop="200dp"
                android:layout_marginLeft="70dp"
                android:layout_width="300dp"
                android:layout_height="300dp"
                android:background="#fcf">
                <! Add view component -->
                <TextView
                    android:id="@+id/dzm"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="100dp"
                    android:layout_marginTop="200dp"
                    android:text="DZM TEST"
                    android:textSize="20sp" />
            </LinearLayout>
        </LinearLayout>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    Copy the code
  • The effect