Android Learning Road (2) Settings view

1. Set the view width and height

​ In Android development, you can use the LayoutParams class to set the width and height of the view (View). LayoutParams is a parameter class for layout, used to specify the position and size of the view in the parent container.
Below is the sample code to set the width and height of the view:

// 创建一个LayoutParams对象
LayoutParams layoutParams = new LayoutParams(width, height);

// 设置视图的LayoutParams参数
view.setLayoutParams(layoutParams);

​ In the above code, width and height respectively represent the width and height of the view to be set, which can be specific pixel values, or can be set using special constants, such as LayoutParams.WRAP_CONTENT means adaptive content size, LayoutParams.MATCH_PARENT means padding parent container.

For example, if you want to set the view's width to 200 pixels and height to 300 pixels, you can use the following code:

// 创建一个LayoutParams对象,设置宽度为200像素,高度为300像素
LayoutParams layoutParams = new LayoutParams(200, 300);

// 设置视图的LayoutParams参数
view.setLayoutParams(layoutParams);

​ You can also set the width of the view through android:layout_width in the xml file, and set the height of the view through android:layout_height.

Through the above methods, you can set the width and height of the view according to your needs.

Step 1: Create Activity: SetBorderActivity.java

Step 2:
Use LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, and fixed-length dp in activity_set_border.xml

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="wrap_content是包裹内容大小"
        android:textColor="#000000"
        android:background="#999999"
        android:textSize="18sp"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="match_parent是填充父容器"
        android:textColor="#000000"
        android:background="#999999"
        android:textSize="18sp"
        />
    <TextView
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="可以选择固定的长度"
        android:textColor="#000000"
        android:background="#999999"
        android:textSize="18sp"
        />

</LinearLayout>

The effect is like this:

We can also implement in java code:

Step 1: Add in the xml file (need to be set to wrap_content)

<TextView
        android:id="@+id/set_border_java"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="在java代码中实现"
        android:textColor="#000000"
        android:background="#999999"
        android:textSize="18sp"
        />

Step 2: In java code

package com.example.module1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TextView;

public class SetBorderActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_border);
        TextView border_java= findViewById(R.id.set_border_java);
        //获取布局参数
        ViewGroup.LayoutParams params= border_java.getLayoutParams();
        params.width=0;
        //设置布局参数
        border_java.setLayoutParams(params);
    }
}

It should be that in Java code, the default unit is px, so we need a tool class to convert dp to px

First we create a Utils and a Utils class

Among them in Utils.java:

package com.example.module1.Utils;

import android.content.Context;

public class Utils {
    //根据手机的分辨率从dp的单位转成为px(像素)
    public  static int dip2px(Context context,float dpValue){
        //获取手机的像素密度(1个px对应几个px)
         float scale= context.getResources().getDisplayMetrics().density;
         return (int) (dpValue*scale+0.5f);
    }
}

In SetBorderActivity.java:

package com.example.module1;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.module1.Utils.Utils;

public class SetBorderActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_border);
        TextView border_java= findViewById(R.id.set_border_java);
        //获取布局参数
        ViewGroup.LayoutParams params= border_java.getLayoutParams();
        //默认单位px单位,需要把dp转化为px;
        params.width= Utils.dip2px(this,300);
        //设置布局参数
        border_java.setLayoutParams(params);
    }
}

Last modified manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication">
        
        <activity
            android:name=".SetBorderActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

The result of the operation is:

Set the spacing of the views

There are two ways to set view spacing:

  • Takes the layout_margin attribute, which specifies the distance between the current view and surrounding sibling views. Including layout_margin, layout_marginLeft, layout_marginTop, layout_marginRight, layout_marginBottom

  • The padding property is used, which specifies the distance between the current view and the internal subordinate views. Including padding, paddingLeft, paddingTop, paddingRight, paddingBottom

Step 1: Create SetMarginActivity.java
Step 2: In the corresponding xml file

<?xml version="1.0" encoding="utf-8"?>
<!--最外层的布局颜色为蓝色-->
<LinearLayout 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="300dp"
    android:orientation="vertical"
    android:background="#00AAFF"
    >
<!--中间层的布局为黄色    -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="20dp"
        android:background="#FFFF99"
        android:padding="60dp"
        >
        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FF0000"></View>
    </LinearLayout>


</LinearLayout>

Open Design:

The width of blue is 20dp, and the width of yellow is 60dp.

Set the alignment of the view

There are two ways to set the alignment of a view:

  • Using the layout_gravity attribute, he specifies the alignment of the current view relative to the superior view.
  • The gravity property is used, which specifies how the subordinate view is aligned relative to the current view.
    ​ The values ​​of layout_gravity and gravity include: left, top, right, bottom, and you can also use vertical lines to connect the values. For example, "left|top" means that it is left and top, that is, it is aligned to the upper left corner.

Step 1: Create an Activity as SetGravityActivity.java
Step 2: In the corresponding xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="300dp"
    android:orientation="horizontal"
    android:background="#ffff99"
    >
<!--    第一个子布局的颜色为红色,它在上级视图中朝下对其,它的下级视图则靠左对其-->
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:layout_weight="1"
        android:background="#ff0000"
        android:layout_margin="10dp"
        android:padding="10dp"
        android:layout_gravity="bottom"
        >
<!--      内部视图的宽度和高度都是100dp,且背景为青色-->
        <View
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="#00ffff"
            ></View>

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:layout_weight="1"
        android:background="#ff0000"
        android:layout_margin="10dp"
        android:padding="10dp"
        android:layout_gravity="top"
        android:gravity="right"
        >
        <View
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="#00ffff"
            ></View>
    </LinearLayout>


</LinearLayout>

Open Design

Guess you like

Origin blog.csdn.net/qq_32907491/article/details/132264332