說起安卓開發(fā),很多小伙伴在剛開始入門的時候會有些云里霧里,覺得很混亂,這很正常,大多數(shù)是因為不太清楚安卓開發(fā)的基本流程,以及各個文件之間是怎樣去相互作用的。我會在這篇文章里面向你介紹一下Android studio工作的基本流程,很基礎(chǔ)很基礎(chǔ)的那種。
1.兩個重要的文件
一個完整的安卓開發(fā)工程,里面的文件有很多很多很多,但是為了說清楚基本的工作流程,我們先關(guān)注兩個重要的文件。比如,當我們先創(chuàng)建一個empyt的工程,android會自動給我們生成已下的一些文件。
記住,左上角要選擇android,才會出現(xiàn)以下的目錄結(jié)構(gòu)
而我們需要關(guān)注的,一個是layout文件下的.xml文件
打開之后,可以看到這樣的畫面
xml文件及其作用
調(diào)整成split視圖,我們不難看出,我們可以在這個xml文件里面來實現(xiàn)界面的布局。仔細觀察,我們可以看到:
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#225252"
android:textColor="#00ff00"
android:layout_gravity="center"
android:gravity="center"
/>
<Button
android:id="@+id/main_bt01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="next"
android:layout_gravity="center"
android:textSize="16dp"
android:background="#300000ff"
/>
類似于這種的代碼,這個就代表了一個一個的組件。而這些組件具體的功能以及一些特性,我會在后面的博客中去介紹,這一次我們先關(guān)注如何跑起來一個安卓程序,并且從一個基礎(chǔ)的角度去了解他的工作流程。
上面我們提到了組件,而組件是什么東西呢?我們打開一個應(yīng)用程序:
比如這個計算器,我們可以看到計算器上面有很多按鈕,還有輸入框等等東西,這些東西我們叫他組件。比如按鈕組件,輸入框組件等等,我們稍作了解。
所以,這里的xml文件是用來實現(xiàn)界面的布局。你想讓你的界面呈現(xiàn)成什么樣的效果,可以在xml文件里面設(shè)置。如果你學(xué)過前端,可以發(fā)現(xiàn)xml文件類似于前端里面的html和css。
MainActivity文件
說完了xml布局文件,現(xiàn)在有一個很重要的問題。我們已經(jīng)將我們的頁面布局設(shè)置好了,并且也寫成了xml文件,那么,我們的布局文件在哪里才能夠調(diào)用顯示出來呢?答案就在我們的MainActivity文件里面。文件目錄如下:
打開之后我們可以看到:(不同版本的Android Studio可能會有一些小小小小小的差異)
下面我就對這段代碼進行一個簡單的介紹
package com.example.test01;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {//繼承AppCompatActivity來調(diào)用安卓程序
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//這里輸入剛剛xml文件的路徑。直接寫R.layout.xxx
}
}
可以看出來,我們是在
setContentView(R.layout.activity_main);//這里輸入剛剛xml文件的路徑。直接寫R.layout.xxx
這行代碼里面去調(diào)用我們剛剛創(chuàng)建的xml布局文件。代碼其余的部分我們后面再詳細介紹。通過這篇文章,我們可以了解到xml布局文件以及如何調(diào)用布局文件。那這里呢,我提供一個小小的代碼,大家可以小試牛刀一下。
xml布局文件:
<?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="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#225252"
android:textColor="#00ff00"
android:layout_gravity="center"
android:gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txt_1"
android:layout_gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/main_bt01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="next"
android:layout_gravity="center"
android:textSize="16dp"
android:background="#300000ff"
/>
</LinearLayout>
MainActivity.java文件:
package com.example.test01;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button button01;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//表示這個activity要顯示哪一個頁面
button01 = (Button) findViewById(R.id.main_bt01);
TextView tv1 = findViewById(R.id.tv1);
button01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tv1.setText("你好,魚皮");
Toast.makeText(MainActivity.this, "魚皮不吃巧克力", Toast.LENGTH_SHORT).show();
}
});
}
}
最后的效果嗯:
文章來源:http://www.zghlxwxcb.cn/news/detail-777565.html
最后總結(jié):最基本的工作流程其實很簡單很簡單。在xml布局文件里面寫我們的布局,在java文件里面去調(diào)用。記住要填寫正確的xml文件路徑,以便于程序能夠找到xml文件所在的位置。文章來源地址http://www.zghlxwxcb.cn/news/detail-777565.html
到了這里,關(guān)于Android Studio基礎(chǔ)工作流程-xml布局文件如何調(diào)用顯示的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!