
出现在Android应用程序屏幕上的所有元素都是视图。 从单个元素(例如文本或按钮)到容器(例如视图组)。 由于涉及的细节很多,因此该主题非常复杂,强烈建议您使用以下方法进一步探讨可用的可能性。 官方文件。 在本教程中,我们将分析最常用的元素,包括视图组和一些基本元素。
查看组
- 线性布局
- 将元素分组为一行,可以是垂直或水平。
- 相对布局
- 元素彼此之间和边距之间进行排列。 它是最灵活,最常用的。
- 滚动视图
- 它用于屏幕上不适合的视图。 它只能包含一个视图或一组视图,并会自动添加滚动条。
- 表格布局
- 将项目分组为行和列。 它包含TableRow元素,而TableRow元素又包含每个单元格的元素。
- 框架布局
- 它旨在包含一个视图。 如果添加更多,它们全部在左上角对齐,重叠。
- 绝对布局
- 从Android 1.5版开始不推荐使用。 在此容器中,从左上角开始以绝对坐标引用元素。 它已被弃用,因为它不能适应不同尺寸的屏幕,而该尺寸从Android 1.5开始就很流行。
对于一个简单的应用程序,要详细查看的最有趣的组是LinearLayout,RelativeLayout和ScrollView。 例如,我们可以 线性布局 垂直包含文本,另一个水平包含按钮:
[html]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width =“ match_parent”
android:layout_height =“ match_parent”
android:direction =“ vertical”>
<文本视图
android:id =“ @ + id / textView1”
android:layout_width =“ wrap_content”
android:layout_height =“ wrap_content”
android:text =“ Text 1” />
<文本视图
android:id =“ @ + id / textView2”
android:layout_width =“ wrap_content”
android:layout_height =“ wrap_content”
android:text =“ Text 2” />
<文本视图
android:id =“ @ + id / textView3”
android:layout_width =“ wrap_content”
android:layout_height =“ wrap_content”
android:text =“ Text 3” />
<文本视图
android:id =“ @ + id / textView4”
android:layout_width =“ wrap_content”
android:layout_height =“ wrap_content”
android:text =“ Text 4” />
[/ html]
[html]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width =“ match_parent”
android:layout_height =“ match_parent”
android:orientation =“ horizontal”>
<按钮
android:id =“ @ + id / button1”
android:layout_width =“ wrap_content”
android:layout_height =“ wrap_content”
android:text =“ Button 1” />
<按钮
android:id =“ @ + id / button2”
android:layout_width =“ wrap_content”
android:layout_height =“ wrap_content”
android:text =“ Button 2” />
<按钮
android:id =“ @ + id / button3”
android:layout_width =“ wrap_content”
android:layout_height =“ wrap_content”
android:text =“ Button 3” />
[/ html]
一 滚动视图 它可以很容易地组成,只需要包装您要编写的容器即可:
android:layout_width =“ fill_parent”
android:layout_height =“ fill_parent”
xmlns:android =“ http://schemas.android.com/apk/res/android”
<...>
[/ html]
<相对布局
xmlns:android =“ http://schemas.android.com/apk/res/android”
android:layout_width =“ match_parent”
android:layout_height =“ match_parent”>
<按钮
android:id =“ @ + id / button1”
android:layout_width =“ wrap_content”
android:layout_height =“ wrap_content”
android:layout_alignParentLeft =“ true”
android:layout_alignParentTop =“ true”
android:text =“ Button 1” />
<按钮
android:id =“ @ + id / button2”
android:layout_width =“ wrap_content”
android:layout_height =“ wrap_content”
android:layout_alignParentRight =“ true”
android:layout_alignParentTop =“ true”
android:layout_toRightOf =“ @ + id / button1”
android:text =“ Button 2” />
[/ html]
在此示例中,第一个按钮与容器的左边缘和上边缘对齐,按钮2与按钮1的上边缘,右边缘和右边缘对齐。
浏览次数
- 文字检视
- 显示固定文本。
- 的EditText
- 包含可编辑的文本。
- 按键
- 简单的按钮。
- 图像按钮
- 使用此按钮可以显示图像而不是文本
- 切换按钮
- 可以保持其按下状态直到再次被按下的按钮。
- 复选框
- 与ToggleButton类似的按钮,用作复选框。
我们之前已经看过TextView的工作原理,因为它只需要包含文本。 在里面 官方文件 我们可以找到更多高级选项,例如更改字体,文本大小,颜色等等。
按钮更加有趣,因为我们必须以某种方式将动作与按钮相关联。 我们将看到两种方式。 在其中之一中,我们将动作直接与活动代码关联:
按钮button =(按钮)findViewById(R.id.button1);
button.setOnClickListener(新的View.OnClickListener(){
public void onClick(视图v){
DisplayToast(“您按下按钮”);
}
});
[/ html]
此处的密钥是我们赋予XML文件中元素的ID,我们需要它才能在代码中找到它。 有了它,我们就可以将所需的动作关联起来。 另一种选择是在按钮XML中包含元素“ android:onClick =“ btnClicked”,然后将具有指定名称的方法直接添加到活动代码中:
[HTML]
public void onClick(视图v){
DisplayToast(“您按下按钮”);
}
[/ html]
对于CheckBox或ToggleButton,我们可以执行与第一种方法类似的操作。 我们通过findViewById获得引用,并应用以下片段:
[HTML]
button.setOnCheckedChangeListener(新的OnCheckedChangeListener(){
@Override public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
如果(已检查)DisplayToast(“您已激活按钮”);
else DisplayToast(“您已禁用按钮”);
}
});
[/ html]
你好母狗我叫山羊
我想和伊凡他妈的
黑鬼的邻居真是个好邻居,一个黑鬼的邻居真不值一分钱,但是它很泥泞
我想他妈的
如何安装APK? 紧急!!
您将apk文件复制到您要安装的终端的sdcard或内部存储器中。 然后,使用任何文件浏览器,输入粘贴的路径,然后单击该路径将自动运行。 如果它返回一个带有放置设置按钮的小窗口,则必须单击它以激活权限,以便能够安装来自未知来源的应用程序。
先生。 Android符号系统是什么意思?
L
您的建筑面积有20米的粉饼,而CHIVA则将其吃光
说的部分
public void onClick(视图v){
DisplayToast(“您按下按钮”);
}
应该
public void btnClicked(查看v){
DisplayToast(“您按下按钮”);
}