Android基础入门教程——2.1 View与ViewGroup的概念
View与ViewGroup的概念
·
1.UI概述
在Android APP中,所有的用户界面元素都是由View和ViewGroup的对象构成的。View是绘制在屏幕上的用户能与之交互的一个对象。而ViewGroup则是一个用于存放其他View(和ViewGroup)对象的布局容器! Android为我们提供了一个View和ViewGroup子类的集合,集合中提供了一些常用的输入控件(比如按钮和文本域)和各种各样的布局模式(比如线性或相对布局)。如下图
2.定义布局
Android的界面至少有一个布局,比如常用的线性布局(LinearLayout),代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是文本载体" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是一个按钮" />
</LinearLayout>
上述代码写法(<LinearLayout></LinearLayout>)和html中的<div></div>的写法是一样的。其中android:orientation="vertical" 表示组件按竖立排放,android:orientation="horizontal"表示横着放。
TextView是文本和Button是按钮,如下图
说到这里,大家也明白了布局(如LinearLayout)就是ViewGroup,TextView和Button就是View。
下一章
更多推荐
所有评论(0)