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)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 android:background="@drawable/background_main" > 7 8 <LinearLayout 9 android:layout_width="match_parent" 10 android:layout_height="45dp" 11 android:background="#393a3e" 12 android:paddingLeft="10dp" 13 android:gravity="left|center_vertical" 14 android:orientation="horizontal" > 15 <TextView 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:textSize="20dp" 19 android:text="幸福苑" 20 /> 21 </LinearLayout> 22 23 <LinearLayout 24 android:id="@+id/id_tab_login" 25 android:layout_width="match_parent" 26 android:layout_height="60dp" 27 android:paddingLeft="10dp" 28 android:layout_marginTop="20dp" 29 android:gravity="left|center_vertical" 30 android:orientation="horizontal" 31 android:background="#ffffff" 32 android:clickable="true" 33 style="?android:attr/actionButtonStyle" > 34 35 <LinearLayout 36 android:layout_width="50dp" 37 android:layout_height="50dp" 38 android:orientation="horizontal" > 39 <ImageButton 40 android:id="@+id/id_tab_login_img" 41 android:layout_width="wrap_content" 42 android:layout_height="wrap_content" 43 android:background="#00000000" 44 android:clickable="false" 45 android:src="@drawable/logo_model" /> 46 </LinearLayout> 47 48 <LinearLayout 49 android:layout_width="match_parent" 50 android:layout_height="50dp" 51 android:gravity="left|center_vertical" 52 android:orientation="vertical" > 53 <TextView 54 android:layout_width="wrap_content" 55 android:layout_height="wrap_content" 56 android:paddingLeft="10dp" 57 android:textColor="#000000" 58 android:text="@string/login_text" 59 /> 60 <TextView 61 android:layout_width="wrap_content" 62 android:layout_height="wrap_content" 63 android:paddingLeft="10dp" 64 android:textColor="#000000" 65 android:textSize="12dp" 66 android:text="开启你的幸福之旅" 67 /> 68 </LinearLayout> 69 </LinearLayout> 70 71 <LinearLayout 72 android:id="@+id/id_tab_pension_consult" 73 android:layout_width="match_parent" 74 android:layout_height="40dp" 75 android:paddingLeft="10dp" 76 android:layout_marginTop="20dp" 77 android:gravity="left|center_vertical" 78 android:orientation="horizontal" 79 android:background="#ffffff" 80 android:clickable="true" > 81 <!-- android:clickable="false" 是为了防止ImageButton截取了触摸事件 ,这里事件要给它的上一级linearlayout--> 82 <ImageButton 83 android:id="@+id/id_tab_pension_consult_img" 84 android:layout_width="wrap_content" 85 android:layout_height="wrap_content" 86 android:background="#00000000" 87 android:clickable="false" 88 android:src="@drawable/logo_model1" /> 89 90 <TextView 91 android:layout_width="wrap_content" 92 android:layout_height="wrap_content" 93 android:paddingLeft="10dp" 94 android:textColor="#000000" 95 android:text="养老咨询" 96 /> 97 </LinearLayout> 98 99 <LinearLayout 100 android:id="@+id/id_tab_server_center" 101 android:layout_width="match_parent" 102 android:layout_height="40dp" 103 android:layout_marginTop="1dp" 104 android:paddingLeft="10dp" 105 android:gravity="left|center_vertical" 106 android:orientation="horizontal" 107 android:background="#ffffff" > 108 <ImageButton 109 android:id="@+id/id_tab_server_center_img" 110 android:layout_width="wrap_content" 111 android:layout_height="wrap_content" 112 android:background="#00000000" 113 android:clickable="false" 114 android:src="@drawable/logo_model1" /> 115 116 <TextView 117 android:layout_width="wrap_content" 118 android:layout_height="wrap_content" 119 android:paddingLeft="10dp" 120 android:textColor="#000000" 121 android:text="服务中心" 122 /> 123 </LinearLayout> 124 125 <LinearLayout 126 android:id="@+id/id_tab_recipe" 127 android:layout_width="match_parent" 128 android:layout_height="40dp" 129 android:layout_marginTop="1dp" 130 android:paddingLeft="10dp" 131 android:gravity="left|center_vertical" 132 android:orientation="horizontal" 133 android:background="#ffffff" 134 style="?android:attr/actionButtonStyle" > 135 <ImageButton 136 android:id="@+id/id_tab_recipe_img" 137 android:layout_width="wrap_content" 138 android:layout_height="wrap_content" 139 android:background="#00000000" 140 android:clickable="false" 141 android:src="@drawable/logo_model1" /> 142 143 <TextView 144 android:layout_width="wrap_content" 145 android:layout_height="wrap_content" 146 android:paddingLeft="10dp" 147 android:textColor="#000000" 148 android:text="营养食谱" 149 /> 150 </LinearLayout> 151 152 <LinearLayout 153 android:id="@+id/id_tab_medical_examination_data" 154 android:layout_width="match_parent" 155 android:layout_height="40dp" 156 android:layout_marginTop="1dp" 157 android:paddingLeft="10dp" 158 android:gravity="left|center_vertical" 159 android:orientation="horizontal" 160 android:background="#ffffff" 161 style="?android:attr/actionButtonStyle" > 162 <ImageButton 163 android:id="@+id/id_tab_medical_examination_data_img" 164 android:layout_width="wrap_content" 165 android:layout_height="wrap_content" 166 android:background="#00000000" 167 android:clickable="false" 168 android:src="@drawable/logo_model1" /> 169 170 <TextView 171 android:layout_width="wrap_content" 172 android:layout_height="wrap_content" 173 android:paddingLeft="10dp" 174 android:textColor="#000000" 175 android:text="体检数据" 176 /> 177 </LinearLayout> 178 179 <LinearLayout 180 android:id="@+id/id_tab_medical" 181 android:layout_width="match_parent" 182 android:layout_height="40dp" 183 android:layout_marginTop="1dp" 184 android:paddingLeft="10dp" 185 android:gravity="left|center_vertical" 186 android:orientation="horizontal" 187 android:background="#ffffff" > 188 <ImageButton 189 android:id="@+id/id_tab_medical_img" 190 android:layout_width="wrap_content" 191 android:layout_height="wrap_content" 192 android:background="#00000000" 193 android:clickable="false" 194 android:src="@drawable/logo_model1" /> 195 196 <TextView 197 android:layout_width="wrap_content" 198 android:layout_height="wrap_content" 199 android:paddingLeft="10dp" 200 android:textColor="#000000" 201 android:text="医疗问诊" 202 /> 203 </LinearLayout> 204 205 <LinearLayout 206 android:id="@+id/id_tab_community_video_monitoring" 207 android:layout_width="match_parent" 208 android:layout_height="40dp" 209 android:layout_marginTop="1dp" 210 android:paddingLeft="10dp" 211 android:gravity="left|center_vertical" 212 android:orientation="horizontal" 213 android:background="#ffffff" > 214 <ImageButton 215 android:id="@+id/id_tab_community_video_monitoring_img" 216 android:layout_width="wrap_content" 217 android:layout_height="wrap_content" 218 android:background="#00000000" 219 android:clickable="false" 220 android:src="@drawable/logo_model1" /> 221 222 <TextView 223 android:layout_width="wrap_content" 224 android:layout_height="wrap_content" 225 android:paddingLeft="10dp" 226 android:textColor="#000000" 227 android:text="社区养老视频监控" 228 /> 229 </LinearLayout> 230 231 <LinearLayout 232 android:id="@+id/id_tab_feedback" 233 android:layout_width="match_parent" 234 android:layout_height="40dp" 235 android:paddingLeft="10dp" 236 android:layout_marginTop="1dp" 237 android:gravity="left|center_vertical" 238 android:orientation="horizontal" 239 android:background="#ffffff" > 240 <ImageButton 241 android:id="@+id/id_tab_feedback_img" 242 android:layout_width="wrap_content" 243 android:layout_height="wrap_content" 244 android:background="#00000000" 245 android:clickable="false" 246 android:src="@drawable/logo_model1" /> 247 248 <TextView 249 android:layout_width="wrap_content" 250 android:layout_height="wrap_content" 251 android:paddingLeft="10dp" 252 android:textColor="#000000" 253 android:text="意见反馈" 254 /> 255 </LinearLayout> 256 257 </LinearLayout>
点击事件的添加方法(HomeActivity.java)
1 package com.test; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.view.Window; 8 import android.widget.LinearLayout; 9 10 public class HomeActivity extends Activity implements 11 android.view.View.OnClickListener { 12 13 //四个Tab,每个Tab包含一个按钮 14 private LinearLayout myTabLogin; 15 private LinearLayout myTabPensionConsulting; 16 private LinearLayout myTabServerCenter; 17 private LinearLayout myTabRecipe; 18 private LinearLayout myTabMedicalExaminationData; 19 private LinearLayout myTabMedical; 20 private LinearLayout myTabCommunityVideoMonitoring; 21 private LinearLayout myTabFeedback; 22 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏 26 setContentView(R.layout.home); 27 init(); 28 } 29 30 /* 31 * 初始化设置 32 */ 33 private void init() { 34 //初始化 LinearLayout 35 myTabLogin = (LinearLayout) findViewById(R.id.id_tab_login); 36 myTabPensionConsulting = (LinearLayout) findViewById(R.id.id_tab_pension_consult); 37 myTabServerCenter = (LinearLayout) findViewById(R.id.id_tab_server_center); 38 myTabRecipe = (LinearLayout) findViewById(R.id.id_tab_recipe); 39 myTabMedicalExaminationData = (LinearLayout) findViewById(R.id.id_tab_medical_examination_data); 40 myTabMedical = (LinearLayout) findViewById(R.id.id_tab_medical); 41 myTabCommunityVideoMonitoring = (LinearLayout) findViewById(R.id.id_tab_community_video_monitoring); 42 myTabFeedback = (LinearLayout) findViewById(R.id.id_tab_feedback); 43 /* 44 * 监听并处理点击事件 45 * 具体的处理方法在onClick(View v)方法中 46 */ 47 myTabLogin.setOnClickListener(this); 48 myTabPensionConsulting.setOnClickListener(this); 49 myTabServerCenter.setOnClickListener(this); 50 myTabRecipe.setOnClickListener(this); 51 myTabMedicalExaminationData.setOnClickListener(this); 52 myTabMedical.setOnClickListener(this); 53 myTabCommunityVideoMonitoring.setOnClickListener(this); 54 myTabFeedback.setOnClickListener(this); 55 } 56 57 public void onClick(View v) { 58 // TODO Auto-generated method stub 59 switch (v.getId()) { 60 case R.id.id_tab_login: 61 Intent intent = new Intent(HomeActivity.this, LoginActivity.class); 62 HomeActivity.this.startActivity(intent); 63 overridePendingTransition(R.layout.out_style, R.layout.in_style); 64 HomeActivity.this.finish(); 65 break; 66 case R.id.id_tab_medical_examination_data: 67 Intent intent1 = new Intent(HomeActivity.this, MedicalExamDataActivity.class); 68 HomeActivity.this.startActivity(intent1); 69 overridePendingTransition(R.layout.out_style, R.layout.in_style); 70 HomeActivity.this.finish(); 71 break; 72 case R.id.id_tab_recipe: 73 Intent intent11 = new Intent(HomeActivity.this, RecipeActivity.class); 74 HomeActivity.this.startActivity(intent11); 75 overridePendingTransition(R.layout.out_style, R.layout.in_style); 76 HomeActivity.this.finish(); 77 break; 78 case R.id.id_tab_pension_consult: 79 Intent intent2 = new Intent(HomeActivity.this, TodoActivity.class); 80 HomeActivity.this.startActivity(intent2); 81 overridePendingTransition(R.layout.out_style, R.layout.in_style); 82 HomeActivity.this.finish(); 83 break; 84 case R.id.id_tab_server_center: 85 Intent intent22 = new Intent(HomeActivity.this, ServerCenterActivity.class); 86 HomeActivity.this.startActivity(intent22); 87 overridePendingTransition(R.layout.out_style, R.layout.in_style); 88 HomeActivity.this.finish(); 89 break; 90 case R.id.id_tab_medical: 91 Intent intent3 = new Intent(HomeActivity.this, MedicalActivity.class); 92 HomeActivity.this.startActivity(intent3); 93 overridePendingTransition(R.layout.out_style, R.layout.in_style); 94 HomeActivity.this.finish(); 95 break; 96 case R.id.id_tab_community_video_monitoring: 97 Intent intent33 = new Intent(HomeActivity.this, TodoActivity.class); 98 HomeActivity.this.startActivity(intent33); 99 overridePendingTransition(R.layout.out_style, R.layout.in_style); 100 HomeActivity.this.finish(); 101 break; 102 case R.id.id_tab_feedback: 103 Intent intent4 = new Intent(HomeActivity.this, TodoActivity.class); 104 HomeActivity.this.startActivity(intent4); 105 overridePendingTransition(R.layout.out_style, R.layout.in_style); 106 HomeActivity.this.finish(); 107 break; 108 default: 109 break; 110 } 111 } 112 113 }