Android基础控件说明及使用

Android基础控件说明及使⽤
Android 基础控件说明及使⽤
⼀、在 Windows 下搭建 Android 开发环境
1、安装 JDK (Java Development Kit)
2、安装 Android SDK
3、安装 Eclipse
4、打开 Eclipse ,并安装其 Android 插件(ADT)
5、新建 Android 项⽬
背板制作“New” -> Android Project,Project Name - 项⽬名称;Build Target - 编译项⽬的 SDK 版本;Application name - 程序名称;Package name - 包名;Min SDK Version - 程序所⽀持的最低 SDK 版本代号(2 对应 1.1,3 对应 1.5,4 对应 1.6)
6、运⾏ Android 项⽬
打开菜单 “Run” -> “Run Configurations” -> New launch configuration,设置启动项⽬名称,在 Android 选项卡中选择启动项⽬,在 Target 选项卡中设置模拟器
7、创建/使⽤模拟 SD 卡
创建 SD 卡,运⾏类似如下命令:mksdcard -l sdcard 512M d:\android\sdcard.img
模拟器中使⽤ SD 卡,在项⽬配置的 Target 选项卡的 “Additional Emulator Command Line Options” 框中输⼊类似如下参数:-sdcard d:\android\sdcard.img
8、配置模拟器
运⾏类似如下命令:android create avd --name android15 --target 2。或者直接在菜单 “Window” -> “Android AVD Manager”中配置模拟器
9、浏览模拟 SD 卡中的内容
调试程序,在 DDMS 中选择 “File Explorer” ,在其中的 sdcard ⽬录下就是模拟 SD 卡中的内容
10、查看⽇志 LogCat
Window -> Show View -> Other -> Android -> LogCat
11、在模拟器中安装/卸载 apk
安装 apk 运⾏类似如下命令:adb install name.apk;卸载 apk 运⾏类似如下命令:adb uninstall packagename(注:这⾥的参数是需要卸载的包名)
12、反编译 Android 程序
解压 apk ⽂件,取出其中的 classes.dex ⽂件,运⾏类似如下命令: -d classes.dex > (其意思是将
classes.dex dump 出来,并将反编译后的代码保存到指定的⽂本⽂件中)
13、⼈品不好是出现的某些错误的解决办法
如果出现类似如下的错误等
no classfiles specified
Conversion to Dalvik format failed with error 1机柜空调器
解决办法:Project -> Clean
出现 Android SDK Content Loader 60% (⼀直卡在 60%)
解决办法:Project -> 去掉 Build Automatically 前⾯的勾
14、查看 SDK 源代码
⼆、Android 项⽬的⽬录结构
1、src - ⽤于放置源程序
2、gen - ⾃动⽣成 R.java ⽂件,⽤于引⽤资源⽂件(即 res ⽬录下的数据)
3、assets - ⽤于放置原始⽂件,Android 不会对此⽬录下的⽂件做任何处理,这是其与 res ⽬录不同的地⽅
4、res/drawable - ⽤于放置图⽚之类的资源;res/layout - ⽤于放置布局⽤的 xml ⽂件;res/values - ⽤于放置⼀些常量数据
5、l - Android 程序的清单⽂件,相当于配置⽂件,配置应⽤程序名称、图标、Ac
tivity、Service、Receiver
三、Hello World 程序
1、res/l
<?xml version="1.0" encoding="utf-8"?>
<!--
设置 ID 的⽅式:ID前加前缀,@+id/
引⽤资源⽂件内字符串资源的⽅式:指定的资源名称前加前缀,@string/
-->
<LinearLayout xmlns:android="schemas.android/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/layout"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txt"
/>
</LinearLayout>
2、res/l
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">layout 直接调⽤ values 中的字符串</string>
<string name="hello2">编程⽅式调⽤ values 中的字符串</string>
<string name="app_name">webabcd_hello</string>
</resources>
3、res/drawable ⽬录下放置⼀个名为 icon.png 的图⽚⽂件
4、l
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="schemas.android/apk/res/android"
package="com.webabcd.hello"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Main"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
5、Main.java
package com.webabcd.hello;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;防盗监控系统
import android.widget.TextView;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
// 将指定的布局⽂件作为 Activity 所显⽰的内容
setContentView(R.layout.main);
// 动态地在指定的容器控件上添加新的控件
TextView txt = new TextView(this);
txt.setText("动态添加控件");
// setContentView(txt);
((LinearLayout)this.findViewById(R.id.layout)).addView(txt);
// 引⽤资源⽂件内的内容作为输出内容
TextView txt1 = (TextView)this.findViewById();
txt1.String(R.string.hello2));
}
双生筷
}
四、布局(Layout)和菜单(Menu)
介绍在 Android 中各种布局的应⽤,以及菜单效果的实现
各种布局⽅式的应⽤,FrameLayout, LinearLayout, TableLayout, AbsoluteLayout, RelativeLayout
为指定元素配置上下⽂菜单,为应⽤程序配置选项菜单,以及多级菜单的实现
1、各种布局⽅式的演⽰(FrameLayout, LinearLayout, TableLayout, AbsoluteLayout, RelativeLayout)
res/l
<?xml version="1.0" encoding="utf-8"?>
<!--
<!--
layout_width - 宽。fill_parent: 宽度跟着⽗元素⾛;wrap_content: 宽度跟着本⾝的内容⾛;直接指定⼀个 px 值来设置宽
layout_height - ⾼。fill_parent: ⾼度跟着⽗元素⾛;wrap_content: ⾼度跟着本⾝的内容⾛;直接指定⼀个 px 值来设置⾼
-->
<!--
LinearLayout - 线形布局。
orientation - 容器内元素的排列⽅式。vertical: ⼦元素们垂直排列;horizontal: ⼦元素们⽔平排列
gravity - 内容的排列形式。常⽤的有 top, bottom, left, right, center 等,详见⽂档
-->
<LinearLayout xmlns:android="schemas.android/apk/res/android"
android:orientation="vertical" android:gravity="right"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<!--
FrameLayout - 层叠式布局。以左上⾓为起点,将  FrameLayout 内的元素⼀层覆盖⼀层地显⽰
-->
<FrameLayout android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="FrameLayout">
</TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Frame Layout">
</TextView>
</FrameLayout>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/hello" />
<!--
TableLayout - 表格式布局。
TableRow - 表格内的⾏,⾏内每⼀个元素算作⼀列
collapseColumns - 设置 TableLayout 内的 TableRow 中需要隐藏的列的列索引,多个⽤“,”隔开
stretchColumns - 设置 TableLayout 内的 TableRow 中需要拉伸(该列会拉伸到所有可⽤空间)的列的列索引,多个⽤“,”隔开
shrinkColumns - 设置 TableLayout 内的 TableRow 中需要收缩(为了使其他列不会被挤到屏幕外,此列会⾃动收缩)的列的列索引,多个⽤“,”隔开    -->
<TableLayout android:id="@+id/TableLayout01"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:collapseColumns="1">
<TableRow android:id="@+id/TableRow01" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_weight="1" android:layout_height="wrap_content"
android:text="⾏1列1" />
<TextView android:layout_width="wrap_content"
android:layout_weight="1" android:layout_height="wrap_content"
android:text="⾏1列2" />
<TextView android:layout_width="wrap_content"
android:layout_weight="1" android:layout_height="wrap_content"
android:text="⾏1列3" />
</TableRow>
<TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="⾏2列1" />
</TableRow>
</TableLayout>
<!--
AbsoluteLayout - 绝对定位布局。
layout_x - x 坐标。以左上⾓为顶点
layout_y - y 坐标。以左上⾓为顶点
-->
<AbsoluteLayout android:layout_height="wrap_content"
android:layout_width="fill_parent">
android:layout_width="fill_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="AbsoluteLayout"
android:layout_x="100px"
android:layout_y="100px" />
</AbsoluteLayout>
<!--
RelativeLayout - 相对定位布局。
layout_centerInParent - 将当前元素放置到其容器内的⽔平⽅向和垂直⽅向的中央位置(类似的属性有:layout_centerHorizontal, layout_alignParentLeft 等)
layout_marginLeft - 设置当前元素相对于其容器的左侧边缘的距离
layout_below - 放置当前元素到指定的元素的下⾯
layout_alignRight - 当前元素与指定的元素右对齐
节能减排设备-->
<RelativeLayout android:id="@+id/RelativeLayout01"
gammaproteobacteriaandroid:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content" android:id="@+id/abc"
android:layout_height="wrap_content" android:text="centerInParent=true"
android:layout_centerInParent="true" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="marginLeft=20px"
android:layout_marginLeft="20px" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="xxx"
android:layout_below="@id/abc" android:layout_alignRight="@id/abc" />
</RelativeLayout>
</LinearLayout>
res/l
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello Layout</string>
<string name="app_name">webabcd_layout</string>
</resources>
Main.java
package com.webabcd.layout;
import android.app.Activity;
import android.os.Bundle;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
}
}
2、上下⽂菜单,选项菜单,⼦菜单
res/l

本文发布于:2024-09-22 20:29:29,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/2/277041.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:元素   指定   菜单   类似   放置   配置
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议