編輯:Android編程入門
Android注意事項
1、編寫Activity.java文件時一定要在AndroidManifest.xml中注冊一下才能正常運行
2、res/layout目錄中的xml文件名一定要使用小寫字母命名
3、需要使用自定義的顏色值和自定義的字符串(例如特殊字體:帶下劃線的文字)時,只需要在res/values 目錄中定義即可,例如:
1)自定義color >>添加一個colors.xml文件
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorgrey">#6B6B6B</color> </resources>
2)自定義strings >>添加strings.xml文件
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, TestActivity!</string> <string name="app_name">appName</string> <string name="protocol"><u>《***軟件許可協議》</u></string> </resources>
4、不要亂往res目錄下的文件夾中添加內容,因為這些文件會在R.java文件中被引用,稍有不慎就會出現很難發現的錯誤,導致程序無法運行,浪費時間。
5、布局文件中常用的標簽有以下幾個,要靈活使用
<RelativeLayout ></RelativeLayout>
<LinearLayout ></LinearLayout>
<ImageButton />
<ImageView />
<TextView />
示例:以下布局代碼可實現帶圖標的可點擊按鈕(home.xml)
<?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="vertical" android:background="@drawable/background_main" > <LinearLayout android:layout_width="match_parent" android:layout_height="45dp" android:background="#393a3e" android:paddingLeft="10dp" android:gravity="left|center_vertical" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" android:text="幸福苑" /> </LinearLayout> <LinearLayout android:id="@+id/id_tab_login" android:layout_width="match_parent" android:layout_height="60dp" android:paddingLeft="10dp" android:layout_marginTop="20dp" android:gravity="left|center_vertical" android:orientation="horizontal" android:background="#ffffff" android:clickable="true" > <LinearLayout android:layout_width="50dp" android:layout_height="50dp" android:orientation="horizontal" > <ImageButton android:id="@+id/id_tab_login_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:clickable="false" android:src="@drawable/logo_model" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:gravity="left|center_vertical" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:textColor="#000000" android:text="@string/login_text" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:textColor="#000000" android:textSize="12dp" android:text="開啟你的幸福之旅" /> </LinearLayout> </LinearLayout> <LinearLayout android:id="@+id/id_tab_pension_consult" android:layout_width="match_parent" android:layout_height="40dp" android:paddingLeft="10dp" android:layout_marginTop="20dp" android:gravity="left|center_vertical" android:orientation="horizontal" android:background="#ffffff" android:clickable="true" > <!-- android:clickable="false" 是為了防止ImageButton截取了觸摸事件 ,這裡事件要給它的上一級linearlayout--> <ImageButton android:id="@+id/id_tab_pension_consult_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:clickable="false" android:src="@drawable/logo_model1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:textColor="#000000" android:text="養老咨詢" /> </LinearLayout> <LinearLayout android:id="@+id/id_tab_server_center" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginTop="1dp" android:paddingLeft="10dp" android:gravity="left|center_vertical" android:orientation="horizontal" android:background="#ffffff" > <ImageButton android:id="@+id/id_tab_server_center_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:clickable="false" android:src="@drawable/logo_model1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:textColor="#000000" android:text="服務中心" /> </LinearLayout> <LinearLayout android:id="@+id/id_tab_recipe" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginTop="1dp" android:paddingLeft="10dp" android:gravity="left|center_vertical" android:orientation="horizontal" android:background="#ffffff" > <ImageButton android:id="@+id/id_tab_recipe_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:clickable="false" android:src="@drawable/logo_model1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:textColor="#000000" android:text="營養食譜" /> </LinearLayout> <LinearLayout android:id="@+id/id_tab_medical_examination_data" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginTop="1dp" android:paddingLeft="10dp" android:gravity="left|center_vertical" android:orientation="horizontal" android:background="#ffffff" > <ImageButton android:id="@+id/id_tab_medical_examination_data_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:clickable="false" android:src="@drawable/logo_model1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:textColor="#000000" android:text="體檢數據" /> </LinearLayout> <LinearLayout android:id="@+id/id_tab_medical" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginTop="1dp" android:paddingLeft="10dp" android:gravity="left|center_vertical" android:orientation="horizontal" android:background="#ffffff" > <ImageButton android:id="@+id/id_tab_medical_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:clickable="false" android:src="@drawable/logo_model1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:textColor="#000000" android:text="醫療問診" /> </LinearLayout> <LinearLayout android:id="@+id/id_tab_community_video_monitoring" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginTop="1dp" android:paddingLeft="10dp" android:gravity="left|center_vertical" android:orientation="horizontal" android:background="#ffffff" > <ImageButton android:id="@+id/id_tab_community_video_monitoring_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:clickable="false" android:src="@drawable/logo_model1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:textColor="#000000" android:text="社區養老視頻監控" /> </LinearLayout> <LinearLayout android:id="@+id/id_tab_feedback" android:layout_width="match_parent" android:layout_height="40dp" android:paddingLeft="10dp" android:layout_marginTop="1dp" android:gravity="left|center_vertical" android:orientation="horizontal" android:background="#ffffff" > <ImageButton android:id="@+id/id_tab_feedback_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:clickable="false" android:src="@drawable/logo_model1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="10dp" android:textColor="#000000" android:text="意見反饋" /> </LinearLayout> </LinearLayout>
點擊事件的添加方法(HomeActivity.java)
package com.test; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.Window; import android.widget.LinearLayout; public class HomeActivity extends Activity implements android.view.View.OnClickListener { //四個Tab,每個Tab包含一個按鈕 private LinearLayout myTabLogin; private LinearLayout myTabPensionConsulting; private LinearLayout myTabServerCenter; private LinearLayout myTabRecipe; private LinearLayout myTabMedicalExaminationData; private LinearLayout myTabMedical; private LinearLayout myTabCommunityVideoMonitoring; private LinearLayout myTabFeedback; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉標題欄 setContentView(R.layout.home); init(); } /* * 初始化設置 */ private void init() { //初始化 LinearLayout myTabLogin = (LinearLayout) findViewById(R.id.id_tab_login); myTabPensionConsulting = (LinearLayout) findViewById(R.id.id_tab_pension_consult); myTabServerCenter = (LinearLayout) findViewById(R.id.id_tab_server_center); myTabRecipe = (LinearLayout) findViewById(R.id.id_tab_recipe); myTabMedicalExaminationData = (LinearLayout) findViewById(R.id.id_tab_medical_examination_data); myTabMedical = (LinearLayout) findViewById(R.id.id_tab_medical); myTabCommunityVideoMonitoring = (LinearLayout) findViewById(R.id.id_tab_community_video_monitoring); myTabFeedback = (LinearLayout) findViewById(R.id.id_tab_feedback); /* * 監聽並處理點擊事件 * 具體的處理方法在onClick(View v)方法中 */ myTabLogin.setOnClickListener(this); myTabPensionConsulting.setOnClickListener(this); myTabServerCenter.setOnClickListener(this); myTabRecipe.setOnClickListener(this); myTabMedicalExaminationData.setOnClickListener(this); myTabMedical.setOnClickListener(this); myTabCommunityVideoMonitoring.setOnClickListener(this); myTabFeedback.setOnClickListener(this); } public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.id_tab_login: Intent intent = new Intent(HomeActivity.this, LoginActivity.class); HomeActivity.this.startActivity(intent); overridePendingTransition(R.layout.out_style, R.layout.in_style); HomeActivity.this.finish(); break; case R.id.id_tab_medical_examination_data: Intent intent1 = new Intent(HomeActivity.this, MedicalExamDataActivity.class); HomeActivity.this.startActivity(intent1); overridePendingTransition(R.layout.out_style, R.layout.in_style); HomeActivity.this.finish(); break; case R.id.id_tab_recipe: Intent intent11 = new Intent(HomeActivity.this, RecipeActivity.class); HomeActivity.this.startActivity(intent11); overridePendingTransition(R.layout.out_style, R.layout.in_style); HomeActivity.this.finish(); break; case R.id.id_tab_pension_consult: Intent intent2 = new Intent(HomeActivity.this, TodoActivity.class); HomeActivity.this.startActivity(intent2); overridePendingTransition(R.layout.out_style, R.layout.in_style); HomeActivity.this.finish(); break; case R.id.id_tab_server_center: Intent intent22 = new Intent(HomeActivity.this, ServerCenterActivity.class); HomeActivity.this.startActivity(intent22); overridePendingTransition(R.layout.out_style, R.layout.in_style); HomeActivity.this.finish(); break; case R.id.id_tab_medical: Intent intent3 = new Intent(HomeActivity.this, MedicalActivity.class); HomeActivity.this.startActivity(intent3); overridePendingTransition(R.layout.out_style, R.layout.in_style); HomeActivity.this.finish(); break; case R.id.id_tab_community_video_monitoring: Intent intent33 = new Intent(HomeActivity.this, TodoActivity.class); HomeActivity.this.startActivity(intent33); overridePendingTransition(R.layout.out_style, R.layout.in_style); HomeActivity.this.finish(); break; case R.id.id_tab_feedback: Intent intent4 = new Intent(HomeActivity.this, TodoActivity.class); HomeActivity.this.startActivity(intent4); overridePendingTransition(R.layout.out_style, R.layout.in_style); HomeActivity.this.finish(); break; default: break; } } }
前言 這是“基礎自測”系列的第三篇文章,以Android開發需要熟悉的20個技術點為切入點,本
注: 本文提到的所有三種滑動方式的完整demo:ScrollDemo1. 關於View我們需要知道的(1)什麼是View? Android中的V
本文介紹Android Studio(下面簡稱AS)中git工具的一些簡單使用。因為AS為git的使用提供了很多人性化的圖形界面操作,在很大程度上可以增加開發效率。本文面
PS:寫一發關於Activity的生命周期,也算是面試的重點內容. 學習內容:1.Activity的生命周期2.面對多種情況的時候Activity的生命周期3.