創建一個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