Android 学习01

一、Android的UI布局方式

1.使用XML文件布局

  在Android应用的res/layout目录下编写XML布局文件: activity_main.xml 

<?xml version="1.0" encoding="utf-8"?>
<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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="38dp"
        android:text="开心消消乐!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.01" />
</androidx.constraintlayout.widget.ConstraintLayout>

  在MainActivity.java中,通过如下代码引入:

package com.kevin.happydispear;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);    // 引入布局文件
        Log.d("MainActivity","onCreate execute");   // 使用日志调试
    }
}

  2.使用Java代码布局

猜你喜欢

转载自www.cnblogs.com/kevinOnes1/p/11231159.html
今日推荐