如何創建android studio項目及線性佈局

文章偏長,建議收藏再看哦!
喜歡的點個贊嘻嘻。

第一步,打開android studio

在這裏插入圖片描述

第二步:建立一個空的Activity

在這裏插入圖片描述

第三步:配置項目

在這裏插入圖片描述
項目名(name):這個自己什麼項目不知道嗎?不知道的拉出去鞭屍

Packages name命名規則:(這裏忽視我的命名規則嘻嘻)

一般命名規則:

com.公司名.項目名.模塊名....

對於個人項目,分爲:

1.individual(個體):
指個體項目,由個人發起,但非個人獨立完成,可公開或私有項目,版權屬於發起人

形式:indi.發起者名.項目名.模塊名....

2.personal (個人私人):
指個人項目,由個人發起,並獨立完成,可分享的項目,版權屬於個人

形式:pers.個人.項目名.模塊名....

3.private(私人):
指私有項目,由個人發起並獨立完成,不用於分享的非公開項目,版權屬於個人

形式:priv.個人.項目名.模塊名....

SaveLocation ( 保存項目地址 ):

一般建立一個:workspace目錄存放

Language

語言的話,我這裏選擇的是kotlin,畢竟是谷歌推薦的官方語言,以後也是趨勢。當然java也不能丟下。

This project will support instant apps:

這個勾選上,在這個項目生成App內部可以通過appLink直接鏈接到其他App。

Use AndroidX artifacts:

使用AndroidX的API支持庫。這個需要勾選上,不然很多對以前的支持包使用時會報錯,最新的Android Studio默認是勾選上的。AndroidX是對Android Support Library的升級。Android Support Library中的API,它們的包名都是在android.support.*下面的,而AndroidX庫中所有API的包名都變成了在androidx.*下面。Android Support Library有一些API支持4.0以前版本。但是現在大多數機器已經是4.0以後版本了,所以可以使用新的AndroidX的API支持庫,已有應用只需要修改導包路徑即可。

點擊右上角Run執行按鈕,沒有找到虛擬機,需要創建。
在這裏插入圖片描述
這裏選中這個圖標
在這裏插入圖片描述
點擊Creat New Virtual Device

在這裏插入圖片描述
這裏是選擇你所需要的機型
在這裏插入圖片描述
這裏選擇手機的系統版本,我選擇的是android 10.0最新的

在這裏插入圖片描述
最後可以給你的設備起個名字,finish。
在這裏插入圖片描述
我一般這樣創建一個新的Activity,因爲懶。。。不用配置
在這裏插入圖片描述

勾選Generate Layout File是什麼意思?

表示會自動爲FirstActivity創建一個對應的佈局文件。

勾選Launcher Activity是什麼意思?

表示會自動將FirstActivity設置爲當前項目的主活動。

直接finish就好。

Andriod佈局管理詳情(1)–LinearLayout線性佈局

*Andriod的佈局方式共有6種,分別是LinearLayout(線性佈局)、TableLayout(表格佈局)、FrameLayout(幀佈局)、RelativeLayout(相對佈局)、GridLayout(網格佈局)以及AbsoluteLayout(絕對佈局)。

LinearLayout常用屬性介紹

在這裏插入圖片描述
屬性1:android:orientation 指定線性佈局的方向(水平或者垂直)

屬性2:android:width 線性佈局的容器寬度

屬性3:android:height 線性佈局的容器高度

屬性4:android:background 線性佈局的背景

屬性5:android:gravity 線性佈局中,子容器相對於父容器所在的位置

1屬性值:

android:orientation="horizontal"           指定線性佈局方向:水平

android:orientation="vertical"               指定線性佈局方向:垂直

2.屬性值:

android:width="xxxdp"                          

指定線性佈局的容器寬度爲:xxxdp

android:width="wrap_content"            

 指定線性佈局的容器寬度爲:根據容器內容寬度大小來填充屏幕寬度

android:width="match_parent"            

 指定線性佈局的容器寬度爲:撐滿整個屏幕寬度

3.屬性值:

android:height="xxxdp"                        

 指定線性佈局的容器高度爲:xxxdp

android:height="wrap_content"            

 指定線性佈局的容器高度爲:根據容器內容高度大小來填充屏幕高度

android:height="match_parent"             

指定線性佈局的容器高度爲:撐滿整個屏幕高度

4.屬性值:

android:background="#000"                  

指定線性佈局的背景爲:黑色(rgb顏色)

android:background="@android:color/black"   

指定線性佈局的背景爲:黑色(引用android系統自帶的原始黑色)

andrid:backgrund="@color/colorPrimary"   

指定線性佈局的背景爲:(根據res/color.xml 中的colorPrimary所定義的顏色設置)

5屬性值:

android:gravity="center"      

指定線性佈局中,子容器相對於父容器所在的位置爲:正中心

android:gravity="cente_verticalr"      

指定線性佈局中,子容器相對於父容器所在的位置爲:垂直方向的正中心

android:gravity="center_horizontal"     

 指定線性佈局中,子容器相對於父容器所在的位置爲:水平方向的正中心

android:gravity="left"      

指定線性佈局中,子容器相對於父容器所在的位置爲:最左邊(默認)

android:gravity="right"      

指定線性佈局中,子容器相對於父容器所在的位置爲:最右邊

android:gravity="top"      

指定線性佈局中,子容器相對於父容器所在的位置爲:最上方(默認)

android:gravity="bottom"     

 指定線性佈局中,子容器相對於父容器所在的位置爲:最下方

垂直方向:

<?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:background="#000"
    android:orientation="vertical">
 
    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn1"
        android:textAllCaps="false" />
 
    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn2"
        android:textAllCaps="false" />
 
    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn3"
        android:textAllCaps="false" />
 
</LinearLayout>

在這裏插入圖片描述

水平方向:

<?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:background="#000"
    android:orientation="horizontal">
 
    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn1"
        android:textAllCaps="false" />
 
    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn2"
        android:textAllCaps="false" />
 
    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn3"
        android:textAllCaps="false" />
 
</LinearLayout>

在這裏插入圖片描述
線性方向:水平,可以通過控件的android:layout_gravity屬性的值來改變控件在垂直方向上的位置

<?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:background="#000"
    android:orientation="horizontal">
 
    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:text="btn1"
        android:textAllCaps="false" />
 
    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="btn2"
        android:textAllCaps="false" />
 
    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="btn3"
        android:textAllCaps="false" />
 
</LinearLayout>

在這裏插入圖片描述

線性方向:水平,通過設置控件的android:layout_weight屬性來設置控件與控件之間,在寬度上的權重比例大小,這裏就將EditText控件與Button控件的寬度比例設置爲1:1,前提是必須先將這兩個控件的android:layout_width=“0dp”.

代碼:

<?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="horizontal">
 
    <EditText
        android:id="@+id/et1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="Type something" />
 
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="send"
        android:textAllCaps="false" />
 
</LinearLayout>

在這裏插入圖片描述

如果只是將EditText控件的

android:layout_width=“0dp”

android:layout_weight=“1”

而Button控件的

android:layout_width="wrap_content“”

android:layout_weight屬性不設置(注意這裏沒設置了)

<?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="horizontal">
 
    <EditText
        android:id="@+id/et1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="Type something" />
 
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="send"
        android:textAllCaps="false" />
 
</LinearLayout>

在這裏插入圖片描述

關於android:id="@+id/xx"的理解

@+id/height
“@”符號是提示XML解析器應該把後面的字符串解析成標識符號。
“+”符號代表將添加一個標識符號。
“id/”表示這個標識符號回被歸類在“id”下面。
“height"是這個界面主見的“android:id”。
以後的程序中,會使用“R.id.height”來取得這個界面組件。因此”@+id/height"的意思是我們在此創建了名爲“height”的標識符,可以通過這個標識符來控制所對應的界面組件,“R”類會自動配置一個地址給這個界面組件。“R”類的內容,這可以通過查看“R.java”文件得知。

你竟然看完了?????
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章