android学习笔记

来源:互联网 发布:adobe全套软件 编辑:程序博客网 时间:2024/06/10 23:25

Android 学习笔记

View:View 对象通常为按钮或文本字段之类的 UI 小部件
ViewGroup:ViewGroup对象则为不可见的视图容器,他们定义子视图的布局,比如是网格布局还是垂直列表布局。

LiearLayout: 是一个视图组(ViewGroup的子类);

通用属性:
1. android:layout_width
2. android:layout_height
- match_parent 填满父容器的宽度或高度,如果在根视图布局则填满屏幕。
- wrap_content 无具体宽度和高度大小,根据需要缩放视图。

属性:
设置布局方向:android:orientation
- horizontal 水平方向
- vertical 垂直方向

EditText:

通用属性:
android:id 一般为代码中引用定义。
- 视图唯一标识符,ID在初次定义: @+资源类型(id)+加号。例如:@+id/
- xml引用任何资源对象,需要使用 @资源类型(本例中为 id),斜杠、资源名称
例如:@id/resource_name
- 对于字符串、布局资源等其他资源直接@资源类型。

属性:
android:hint 为空时的默认字符串。@string/edit_message 引用的是外部字符串资源文件。由于他引用的是一个具体资源,所以无需使用+号

Button: 按钮

属性:
1. android:text 定义按钮文本显示的内容
2. android:layout_weight 默认为 0
值是一个数字,用于指定每个视图与其他同级视图在剩余空间中的占比。
3. android:onClick 点击事件,系统Activity调用对应事件方法
条件:
- 方法名必须匹配
- 必须是公共的
- 必须是无返回值
- 以 View 作为唯一参数(这将是之前点击的 View)

说明:
android:layout_weight="1" , android:layout_width="0dp"
将宽度设置为零 (0dp) 可提高布局性能,这是因为如果将宽度设置为 “wrap_content”,则会要求系统计算宽度,而该计算最终毫无意义,因为 weight 值还需要计算另一个宽度,才能填满剩余空间

Intent
Intent 构造函数采用两个参数:
- Context 是第一个参数(之所以使用 this ,是因为 Activity 类是 Context 的子类)
- 应用组件的 Class,系统应将 Intent(在本例中,为应启动的 Activity)传递至该类。

putExtra() 以 键值对形式携带数据类型。下一个 Activity 将使用该键来检索文本值。为 Intent extra 定义键时最好使用应用的软件包名称作为前缀。这可以确保在您的应用与其他应用交互过程中这些键始终保持唯一。
startActivity() 方法将启动 Intent 指定的 Activity 的实例。您需要创建该类

第二个Activity

   Intent intent = getIntent();   String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);   TextView textView = new TextView(this);   textView.setTextSize(40);   textView.setText(message);   ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);   layout.addView(textView);
  1. 调用 getIntent() 采集启动 Activity 的 intent。无论用户如何导航到目的地,每个 Activity 都由一个 Intent 调用。 调用 getStringExtra() 将检索第一个 Activity 中的数据。
  2. 您可以编程方式创建 TextView 并设置其大小和消息。
  3. 您可将 TextView 添加到 R.id.activity_display_message 标识的布局。您可将布局投射到 ViewGroup,因为它是所有布局的超类且包含 addView() 方法。

使用平台样式和主题及自定义主题

对话框Activity:

<activity android:theme="@android:style/Theme.Dialog">

透明背景的Activity:

<activity android:theme="@android:style/Theme.Translucent">

自定义主题定义:

  1. 文件:color/color.xml
<color name="custom_theme_color">#b0b0ff</color>
  1. 文件:/res/values/styles.xml
<style name="CustomTheme" parent="android:Theme.Light">    <item name="android:windowBackground">@color/custom_theme_color</item>    <item name="android:colorBackground">@color/custom_theme_color</item></style>

应用运行在 Android 3.0(API 级别 11)或更高版本系统上时使用更新的全息主题:

<style name="LightThemeSelector" parent="android:Theme.Holo.Light">    ...</style>

以下这个声明所对应的自定义主题就是标准的平台默认明亮主题

<style name="LightThemeSelector" parent="android:Theme.Light">    ...</style>

将主题应用到你的整个应用程序(所有活动),添加android:theme 属性的元素:

<application android:theme="@style/CustomTheme">
原创粉丝点击