PS:人不要低估自己的实力,但是也不能高估自己的能力.凡事谦为本...

学习内容:

1.用户界面View中的五大布局...

i.首先介绍一下view的概念

  view是什么呢?我们已经知道一个Activity是Android的显示层,但是Activity是不能直接显示在屏幕上的,它也像JSP那样,显示的东西是html,那么Android也不例外,Activity是一个抽象的壳子,而显示的东西就是view或者是viewgroup(图形用户组件)....

  有了这个概念,我们就清楚view是如何在Activity中进行展示的,Activity调用setcontentview()方法去构建一个根节点,通过这个根节点去绘制一棵树状结构的图..以根节点开始,建立根节点下的直接子节点,这些子节点是同一层次的关系,那么我们再以子节点为根,去绘制子节点的新子节点...然后进行依次类推,一层扣一层,这样就会形成一棵树状结构的层次模型图..那么显示的时候自然要按照这颗树的顺序来进行显示...学过前台的人会很清楚,这个东西就和js中的DOM树是很相似的..思路基本是相通的...

ii.布局的概念

  布局这东西,换成任何一个程序员都会知道什么是布局,就是要给用户显示一个看着既美观又好用的交互界面,这就是布局,有了布局,才能够将做出来的界面更加的美观..Android的布局继承于ViewGroup,ViewGroup是一个特殊的view,它又继承于Android.view.View..它的功能就是装载和管理下一层的View对象或ViewGroup对象,也就说他是一个容纳其它元素的的容器。ViewGroup是布局管理器(layout)及view容器的基类。 ViewGroup中,还定义了一个嵌套类ViewGroup.LayoutParams。这个类定义了一个显示对象的位置、大小等属性,view通过LayoutParams中的这些属性值来告诉父级,它们将如何放置。总而言之,就是为界面提供了一个容器,我们可以将组件放入到容器当中...最终形成一个美观的用户交互界面...

iii.FrameLayout帧布局..

  FrameLayout布局给我的感觉就是用处不是很大,用的也不多,主要是它规定所有的显示对象都固定在窗口的左上角,并且不允许修改位置,允许多个对象进行叠加防止,不过后一个对象会覆盖掉前一个对象..

<!--这里使用了ImageView组件...这个组件是图形组件...先不具体进行讲解,这里添加了三个ImageView组件,src中p1表示图片的名称,这里插入了三张图片..
注意:我们在插入图片的时候,一定要将图片导入到res的drawable文件夹下,res下的drawable文件夹很多,导入到哪一个文件夹都是可以的...-->
<?xml version=”1.0″ encoding=”utf-8″?>
<FrameLayout
android:id="@+id/FrameLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ImageView
android:id="@+id/ImageView01"
android:src="@drawable/p1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<ImageView
android:id="@+id/ImageView02"
android:src="@drawable/p2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<ImageView
android:id="@+id/ImageView03"
android:src="@drawable/p3"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
</FrameLayout>

src下的JAVA文件...

package com.example.android_view;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.TextView;
import android.app.Activity;
import android.widget.ImageView;
import android.app.Activity;
public class MainActivity extends Activity{
private ImageView myImageView_1;
private ImageView myImageView_2;
private ImageView myImageView_3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);//调用父类的onCreate方法去初始化对象...
setContentView(R.layout.activity);//调用setContentView方法..告诉Activity去调用activity.xml文件下的布局...
myImageView_1=(ImageView)findViewById(R.id.ImageView_01);
myImageView_2=(ImageView)findViewById(R.id.ImageView_02);
myImageView_3=(ImageView)findViewById(R.id.ImageView_03);//找到xml文件下的指定id的ImageView...
// if (savedInstanceState == null) {
// getSupportFragmentManager().beginTransaction()
// .add(R.id.container, new PlaceholderFragment()).commit();
// }
} @Override
public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
} /**
* A placeholder fragment containing a simple view.
*/
// public static class PlaceholderFragment extends Fragment {
//
// public PlaceholderFragment() {
// }
//
// @Override
// public View onCreateView(LayoutInflater inflater, ViewGroup container,
// Bundle savedInstanceState) {
// View rootView = inflater.inflate(R.layout.fragment_main, container,
// false);
// return rootView;
// }
// } }

加入了三个图像,这三个图像会相应的进行覆盖...

  补充一个findViewById()的方法...findViewById()方法一共有两种...第一种就是我们上面使用的方法,是Activity里的方法...另一种则是view中的方法...这两种方法是完全不同的...我们来看一下源码..

//Activity中的findViewById()方法...
/**
* Finds a view that was identified by the id attribute from the XML that
* was processed in {@link #onCreate}.
*
* @return The view if found or null otherwise.
*/
public View findViewById(int id) {
return getWindow().findViewById(id);
}
/**
* Retrieve the current {@link android.view.Window} for the activity.
* This can be used to directly access parts of the Window API that
* are not available through Activity/Screen.
*
* @return Window The current window, or null if the activity is not
* visual.
*/
public Window getWindow() {
return mWindow;
}
//这种方法表示的是寻找到xml文件下指定id的对象... //view中的findViewById()方法...
/**
* Look for a child view with the given id. If this view has the given
* id, return this view.
*
* @param id The id to search for.
* @return The view that has the given id in the hierarchy or null
*/
public final View findViewById(int id) {
if (id < 0) {
return null;
}
return findViewTraversal(id);
/**
* {@hide}
* @param id the id of the view to be found
* @return the view of the specified id, null if cannot be found
*/
protected View findViewTraversal(int id) {
if (id == mID) {
return this;
}
return null;
}
//寻找与指定id相同的对象..,所以即使几个layout的XML文件中的View的id号相同的话,只要他们没有相同的父节点,或有相同的父亲节点,但不在父节点及以上节点调用findViewById通过id来查找他们就是没有问题..

两种方法的用处完全是不同的,不要混淆概念...

iv.LinearLayout线性布局...

  线性布局就比较常用了,也是RadioGroup, TabWidget, TableLayout, TableRow, ZoomControls类的父类。LinearLayout可以让它的子元素垂直或水平的方式排成一行(不设置方向的时候默认按照垂直方向排列)。下面是线性布局的代码...java文件和上面一样...就不进行粘贴了..

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView_1"
android:textSize="20px"
android:textColor="#0ff"
android:background="#333">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView_2"
android:textSize="20px"
android:textColor="#0f0"
android:background="#eee"
android:layout_weight="3">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView_3"
android:textColor="#00f"
android:textSize="20px"
android:background="#ccc"
android:layout_weight="1">
</TextView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView_4"
android:textColor="#f33"
android:textSize="20px"
android:background="#888"
android:layout_weight="1">
</TextView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView_5"
android:textColor="#ff3"
android:textSize="20px"
android:background="#333"
android:layout_weight="1">
</TextView>
</LinearLayout>

v.AbsoluteLayout绝对布局...

  绝对布局就是我们直接在屏幕上定义控件所放置的坐标位置,指定其放置的坐标.这种布局简单,直观性很强,但是很不灵活,一旦屏幕的大小不同,那么就会直接导致放置的位置也不一样..在屏幕小放置组件的位置是正中央,但是一旦换了不同大小的屏幕,就不可能在出现在正中央了,对应屏幕的相对位置一定会发生变化...这里的layout_x和layout_y就是直接进行坐标设置...这在不同大小的屏幕上显示的位置一定是不同的,大家可以去试试..

<?xml version=”1.0″ encoding=”utf-8″?>
<AbsoluteLayout
android:id="@+id/AbsoluteLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#fff">
<ImageView
android:src="@drawable/android"
android:layout_y="40dip"
android:layout_width="wrap_content"
android:layout_x="35dip"
android:id="@+id/ImageView01"
android:layout_height="wrap_content">
</ImageView>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/TextView01"
android:text="Android2.2 学习指南"
android:textColor="#0f0"
android:textSize="28dip"
android:layout_y="330dip"
android:layout_x="35dip">
</TextView>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/TextView02"
android:text="图文并茂,理论清晰,操作性强"
android:textColor="#333"
android:textSize="18dip"
android:layout_y="365dip"
android:layout_x="35dip">
</TextView>
</AbsoluteLayout>

vi.RelativeLayout相对布局...

相对布局

  相对布局是很常用的一种布局方式...灵活性很大,相对应的属性也很多...但是也是优缺点的,操作难度比较大,属性之间可能会引起冲突...并且允许子元素指定它的父元素或者是兄弟元素的所在位置...总而言之,使用相对布局要多进行测试...

<?xml version=”1.0″ encoding=”utf-8″?>
<AbsoluteLayout
android:id="@+id/AbsoluteLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#fff">
<ImageView
android:src="@drawable/android"
android:layout_width="wrap_content"
android:layout_marginTop="40dip"
android:id="@+id/ImageView01"
android:layout_height="wrap_content">
</ImageView>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/TextView01"
android:text="Android2.2 学习指南"
android:textColor="#0f0"
android:textSize="28dip"
android:layout_below="@id/ImageView01"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dip">
</TextView>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/TextView02"
android:text="图文并茂,理论清晰,操作性强"
android:textColor="#333"
android:textSize="18dip"
android:layout_below="@id/TextView01"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dip">
</TextView>
</AbsoluteLayout>

   例子就和上面一样,只要把AbsoluteLayout改成RelativeLayout,然后修改一下对应的位置,这里layout_below表示现在这个组件位于哪个组件的下方..layout_marginTop表示的是距离上面组件的距离是多少...这样我们就会发现其效果,就算是在不同大小的屏幕中进行显示,它显示的位置相对于屏幕的位置都是相同的...这就是相对布局更大的一个优势...

vii.TableLayout表格布局...

  表格布局TableLayout以行列的形式管理子元素,每一行是一个TableRow布局对象,当然也可以是普通的View对象,TableRow离每放一个元素就是一列,总列数由列数最多的那一行决定。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"> <!--这个表示的是对应的那一列进行自动伸展,什么是自动伸展呢,比如说一个行内有两个组件,这两个组件的大小是无法填充满第一行的
那么如果其中一个进行伸展,那么就代表这个组件会填满剩下空缺的位置,说白了就是改变其长度...-->
<TableRow>
<!--android:layout_column="1" // 0代表第一列..1代表第二列..这个数字是几,就代表插入的是几+1列..那么这个组件就是第二列,下面那个就是第一列了..
默认是从0开始的...-->
<TextView
android:layout_column="1"
android:text="打开…"
android:padding="3dip"/> <!--组件与边界的距离..-->
<!-- android:gravity="right"//元素的内容向右对其-->
<TextView
android:text="Ctrl-O"
android:gravity="right"
android:padding="3dip"/>
</TableRow> <!--这里一行放了两列组件..-->
<TableRow>
<TextView
android:layout_column="1"
android:text="保存…"
android:padding="3dip"/>
<TextView
android:text="Ctrl-S"
android:gravity="right"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:layout_column="1"
android:text="另存为…"
android:padding="3dip" />
<TextView
android:text="Ctrl-Shift-S"
android:gravity="right"
android:padding="3dip" />
</TableRow>
<View <!--一个View对象独占了一行...--->
android:layout_height="2dip"
android:background="#FF909090"/>
<TableRow>
<TextView
android:text="X"
android:padding="3dip" />
<TextView
android:text="导入…"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:text="X"
android:padding="3dip" />
<TextView
android:text="导出…"
android:padding="3dip" />
<TextView
android:text="Ctrl-E"
android:gravity="right"
android:padding="3dip" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF909090" />
<TableRow>
<TextView
android:layout_column="1"
android:text="退出"
android:padding="3dip" />
</TableRow>
</TableLayout>

TableRow也是一个Layout,里面的元素会水平排列,如果TableRow的父元素不是TableLayout的话,那么他会表现的像一个LinearLayout。

Android学习笔记(第二篇)View中的五大布局的更多相关文章

  1. Android学习笔记(第一篇)编写第一个程序Hello World+Activity

    PS:终于开始正式的搞Android了...无人带的一介菜鸟,我还是自己默默的努力吧... 学习内容: 1.编写第一个Hello World程序..   学习Android,那么就需要有一个编译器来集 ...

  2. Android学习笔记(八)——四种基本布局

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 布局是一种可用于放置很多控件的容器,它可以按照一定的规律调整内部控件的位置,或是嵌套子布局,从而编写出精美的界 ...

  3. Struts2学习笔记 - Action篇<配置文件中使用通配符>

    有三种方法可以使一个Action处理多个请求 动态方法调用DMI 定义逻辑Acton 在配置文件中使用通配符 这里就说一下在配置文件中使用通配符,这里的关键就是struts.xml配置文件,在最简单的 ...

  4. android学习笔记(入门篇)

    +号只是当你第一次定义一个资源ID的时候需要, 告诉SDK此资源ID需要被创建出来 对于所有的View默认的权重是0,如果你只设置了一个View的权重大于0,那么这个View将占据除去别的View本身 ...

  5. js学习笔记第二篇

    Js笔记整理 1.StringAPI a)        大小写转换:str.toUpperCase();str.toLowerCase(); b)        获取指定位置字符: Str[i]-- ...

  6. Node 之 Express 学习笔记 第二篇 Express 4x 骨架详解

    周末,没事就来公司加班继续研究一下Express ,这也许也是单身狗的生活吧. 1.目录结构: bin, 存放启动项目的脚本文件 node_modules, 项目所有依赖的库,以及存放 package ...

  7. Asp.net core Identity + identity server + angular 学习笔记 (第二篇)

    先纠正一下第一篇的的错误. 在 Login.cshtml 和 Login.cshtml.cs 里, 本来应该是 Register 我却写成 Login . cshtml 修改部分 <form a ...

  8. R学习笔记 第二篇:矩阵、数组和列表

    向量是一维的,只有长度(行),没有其他维度.R中存在更高维度的对象,他们是矩阵,数据框,数组.这些对象的下标都是使用中括号[],第一个维度是row,第二个维度是column,依次类推,[row,col ...

  9. 转载 ----Android学习笔记 - 蓝牙篇 (Bluetooth)

      1.什么是蓝牙  Bluetooth是目前使用的最广泛的无线通讯协议之一  主要针对短距离设备通讯(10米)  常用于连接耳机.鼠标和移动通讯设备等 2.发现周围蓝牙设备  BluetoothAd ...

随机推荐

  1. Maven 简单配置gpg

    1. 下载maven到指定目录,指定对应的gpg的执行命令所需要的属性.这里比如下载解压后的maven目录是: C:\maven-apache-3.3.2 ,那么配置文件目录是: C:\maven-a ...

  2. Selenium 3 -how to locate the chromedriver and geckodriver place?

    Maybe you met these exceptions sometimes: 1. Chrome Driver The path to the driver executable must be ...

  3. How to apply Local Group Policy settings silently using the ImportRegPol.exe and Apply_LGPO_Delta.exe utilities.

    参考:http://supportishere.com/how-to-apply-local-group-policy-settings-silently-using-the-importregpol ...

  4. cer pfx格式数字证书区别

    作为文件形式存在的证书一般有这几种格式: 1.带有私钥的证书 由Public Key Cryptography Standards #12,PKCS#12标准定义,包含了公钥和私钥的二进制格式的证书形 ...

  5. Oracle中group by用法

    Oracle中group by用法 在select 语句中可以使用group by 子句将行划分成较小的组,一旦使用分组后select操作的对象变为各个分组后的数据,使用聚组函数返回的是每一个组的汇总 ...

  6. Java Inner Classes

    When thinking about inner classes in java, the first thing that comes to my mind is that, WHY do we ...

  7. RxJava操作符之Share, Publish, Refcount

    原文链接:http://nerds.weddingpartyapp.com/tech/2015/01/21/rxjava-share-publish-refcount-and-all-that-jaz ...

  8. 标准 DateTime 格式字符串

    标准 DateTime 格式字符串 MSDN 标准 DateTime 格式字符串包含一个标准 DateTime 格式说明符字符,该字符表示自定义 DateTime 格式字符串.格式字符串最终定义由格式 ...

  9. JVM性能分析与优化

    JVM性能分析与优化: http://www.docin.com/p-757199232.html

  10. Java Web 工作技巧总结 16.8

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! 四时不谢之兰,百节长青之竹,万古不败之石,千秋不变之人. 1. AOP – LOG项目中,一个请 ...