能夠參考婁老師的博客Android開發簡易教程或者參考《Java和Android開發學習指南》第二十四章,裏面都有詳細步驟,一步一步來就很簡單~html
新建一個project項目後,project窗口主要有兩個主要的節點:app和Gradle Scripts。app節點中包含了應用程序中全部的組件,咱們編寫程序時也是主要使用app。app節點下面有包含3個節點java
manifests
包含一個AndroidManifest.xml
清單文件java
包含了全部的Java應用程序和測試類res
包含了資源文件,在這個目錄下還有一些目錄:
drawable
layout
menu
values
mipmap
以上就初步瞭解了Android Studio的構造android
步驟:android-studio
layout->activity_main.xml
android:text
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.lenovo.myapplication.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=" Hello World! 20165312曹歌 Hello World! 20165311李嘉昕 Hello World! 20165313張晨暉" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
MainActivity.java
調試一個應用程序最簡單的辦法就是使用日誌消息。在開發過程當中,能夠在Android Studio主屏幕的底部看到Android DDMS視圖。關於LogCat,不一樣日誌級別的消息以不一樣的顏色來顯示,每條消息都有一個標籤,這使得很容易找到一條消息。app
Run->Debug
便可進行調試程序活動(activity)是包含了用戶界面組件的一個窗口,一個典型的Android應用程序,都是從啓動一個活動開始的。應用程序所建立的第一個窗口,叫作主活動。appliaction元素定義兩個活動,其中之一使用intent-filter元素聲明的爲主活動。
ide
啓動一個activity涉及實例化活動類,而且調用生命週期方法:佈局
onCreat()
onStart()
onResume()
onPause()
onStop()
onRestart()
onDestory()
每個控制文件的Activity都須要有對應的啓動程序文件(.java)和相應的佈局文件(.xml)學習
教材相關代碼連接測試
步驟:gradle
AndroidManifest.xml
清單文件中添加一個activity
<activity android:name=".ThirdActivity"> </activity>
ThirdActivity.java
文件package cn.edu.besti.is.cg; import android.app.Activity; import android.os.Bundle; public class ThirdActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_third); } }
third_activity.xml
文件<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".ThirdActivity"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="20165312曹歌"/> </RelativeLayout>
MainActivity.java
使其可以自啓動ThirdActivity.java
package cn.edu.besti.is.cg; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.TextView; import static cn.edu.besti.is.cg.R.id.textView1; public class MainActivity extends Activity implements OnTouchListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = (TextView) findViewById(textView1); tv.setOnTouchListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it // is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onTouch(View arg0, MotionEvent event) { Intent intent = new Intent(this, ThirdActivity.class); intent.putExtra("message", "Message from First Screen"); startActivity(intent); return true; } }
所謂UI,就是爲主活動構建用戶交叉(user interface)
Toast:一個消費彈出對話框,用於顯示一條消息做爲給用戶的反饋,Toast並不會代替當前的內容。
修改activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="cn.edu.besti.is.cg.ui.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="歡迎來到20165312的世界" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
修改MainActivity.java
package cn.edu.besti.is.cg.ui; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toast toast = Toast.makeText(MainActivity.this, "20165312曹歌", Toast.LENGTH_LONG); toast.show(); } }
Android中的一些佈局:
LinearLayout
將全部子視圖以相同的方向(水平或者垂直)對齊的一個佈局RelativeLayout
根據子視圖的一個或者多個同級視圖的位置來排列他的一個佈局FrameLayout
將每個子視圖放在另外一個子視圖頂部的一種佈局TableLayout
將子視圖按照行和列來組織的一種佈局GridLayout
將子視圖放置到一個柵格中的一種佈局修改activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:text="Button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:layout_marginLeft="100dp" /> <ImageButton android:src="@android:drawable/btn_star_big_on" android:alpha="0.45" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="200dp" android:layout_marginLeft="300dp" /> </FrameLayout>
MainActivity.java
package cn.edu.besti.is.cg.cellview; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { int counter = 0; int[] colors = { Color.BLACK, Color.BLUE, Color.CYAN, Color.DKGRAY, Color.GRAY, Color.GREEN, Color.LTGRAY, Color.MAGENTA, Color.RED, Color.WHITE, Color.YELLOW }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it // is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } public void changeColor(View view) { if (counter == colors.length) { counter = 0; } view.setBackgroundColor(colors[counter++]); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <AnalogClock android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="90dp" android:id="@+id/analogClock1" android:onClick="changeColor" /> </RelativeLayout>
Executing tasks: app:assembleDebug
的問題,百度以後找到androidStudio出現Executing tasks: app:assembleDebug。將build.gradle(module:app)
文件中
testApplicationId「com.cn.skypiea.test" testInstrumentationRunner "android.test.InstrumentationTestRunner"
註釋以後,就能夠成功打包了。
Execution failed for task ':app:preDebugAndroidTestBuild
的問題,百度以後找到一篇博客,Build->Rebuild Project
以後便可正常運行其實吧我以爲R挺玄乎的,我嘗試過好幾種解決辦法,第一次我從新運行了一遍而後R就自動不標紅了,反正就是挺奇怪的;第二次標紅我修改了package
的內容而後就能夠了;第三次我採起了Ctrl+Enter
新建一個東西就ok了,因此多嘗試。。。
最後我找到了一篇博客 Android studio中R變成紅色studio中R變成紅色供你們參考。。