1. 程式人生 > >建立一個Android專案

建立一個Android專案

建立一個Android專案

1.點選File-New Project

image.png

2.Next-選擇Phone 這裡的SDK選擇Android7.1.1

image.png

3.Add no activity

image.png

4.錯誤解決

我們在建立完後經常會報如下的錯誤:

image.png

這裡首先你需要檢視你的SDK的版本,首先進行更改版本,我這裡更改如下:

image.png

註釋掉junit:

image.png

更改:

image.png

allprojects {
    repositories {
//        jcenter()
        maven { url 'http://repo1.maven.org/maven2' }
    }
}

然後重新引入下相關的gradle即可

5.建立好的檔案結構如下:

image.png

6.在com.example.activity 中建立新的類FirstActivity

image.png

搜狗截圖20181113104402.png

Generate Layout File勾選的話會預設創一個預設的佈局,launcher Activity只的是把當前的類當做主活動

7.在res中新建一個Directory 命名layout 再在該目錄下建一個layout_resource_file,名為first_layout

image.png

image.png

8.修改first_layout .xml檔案,加一個Button

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <Button
        android:id="@+id/button_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button 1"
        />

</LinearLayout>

然後在主類視窗中載入這個按鈕:

package com.example.activitytest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class FirstActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);
    }
}

修改AndroidManifest.xml:

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

    <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/AppTheme">
        <activity android:name=".FirstActivity"
            android:label="this is first"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

9.啟動程式,可以看到如下的畫面,說明你已經建立了一個Android專案了:

image.png