`
java_suddy
  • 浏览: 31189 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

Android学习笔记(一)——布局

阅读更多

     在写笔记之前,我先把前些阶段我一直在学习Android的的一些想法先说下:当时买了一本《精通android 2.0》但是里面东西太多。虽说看了有些日子,但是感觉效率不是很高。

      感觉自己以前学习的方法不是很对。当知道自己效率不高时果断改变学习方法。前些阶段刚接触Android的时候,感觉应该把基础掌握牢(前些面试的时候的感觉),所以在一味研究 基础。自己研究Android SDK的基本原理,当时探讨资源、ContentProvider和Intent。但是感觉还是懵懵懂懂,所以这次我们一个个来,然后再放到一起系统分析整合。

      这里就不说Android的配置了,网上一大堆。重复来重复去也没意思。大家自己配置好。先简单了解下Android分解应用程序与各个模块的功能,如下图大概知道每个快的功能,网上很多。这里不说了。


 


在我分开说基本原理前,我先和大家分享Android的布局,因为这些对于后来举例说明都很有用,都会提到。所以开始吧。


了解了Android布局,发现它和SWING非常相似,在调用各个布局按钮、输入框、文本等信息时,调用的函数都相同。EditText、Button、CheckBox、TextViewRadioButton等。给大家一个简单的实例知道怎么调用输入就行。其实很简单。大家只要在res/layout/main.xml中进行配置就行。一个个调用就用。其他不用。但是Android提供给学习人员LinearLayout(水平或垂直组织其子控件)、TableLayout(以表格形式组织其子空间)、RelativeLayout(以与其他子空间或父控件相对的形式组织其子控件)和Framelayout(支持在布局中动态更改控件)四个布局管理器,大家单从表面的含义都能理解个大概。下面就一个说。

一、LinearLayout

这个是应用最广泛,也是大家在简历project时自动会生成的,其他的可能需要大家自己配置,所以可见他在以后编程应用之广泛。所以就着重介绍下:

这里简单给大家一个例子在res/layout/main.xml进行下面的配置

<?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"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<EditText
	android:id="@+id/edit"
   	android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="输入框"
    />     
    
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="文本"
    />
  
<Button
	android:id="@+id/button"
  	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="按钮" 
     />
<CheckBox
	android:id="@+id/checkBox"
 	android:layout_width="wrap_content"
  	android:layout_height="wrap_content"  
    android:text="单选项" />
  
<RadioButton
    android:id="@+id/radioButton"
    android:layout_width="wrap_content"
  	android:layout_height="wrap_content"  
    android:text="单选按钮" />
<AnalogClock
  	android:layout_width="wrap_content"
  	android:layout_height="wrap_content"  
    android:text="时钟" />
  
</LinearLayout>

 

运行Android模拟器就可显示如下:

 
所以大家看到了其实和swing中调用几乎差不多。这里如果要是调用各个输入框或者按钮。我们只需获得它的ID在src下面的主函数中通过调用findViewById(R.id.所需的id名)就可以调用。

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;

public class LayoutActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        EditText editText = (EditText) findViewById(R.id.edit);
        Button button = (Button) findViewById(R.id.button);
        CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
        RadioButton radioButton = (RadioButton) findViewById(R.id.radioButton);
    }
}

这里对一些必要的代码进行解析:

 

 android:orientation=""

   vertical:  表示垂直布局

   horizontal:表示水平布局

 

 android:layout_width=""

   fill_parent:  表示填满父控件的空白

   wrap_content: 表示大小刚好足够显示当前的控件里的内容

接下来还可能用到

android:gravity=""

   right

   left

   center

   top

   bottom 

   center_vertical   表示纵向延伸

   center_horizontal 表示横向延伸

还有很多如下面,大家最好一个个试一下(这下面也不全)


 

二、TableLayout

 

其实它和CSS中布局的Table很相近,就是表格,我把刚才就代码变一下:

 

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
 <TableRow>
    <TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="文本"
    />
	<EditText
	android:id="@+id/edit"
   	android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="输入框"
    />       
  </TableRow>
  <TableRow>
<Button
	android:
	android:id="@+id/button"
  	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="按钮" 
     />
<CheckBox
	android:id="@+id/checkBox"
 	android:layout_width="wrap_content"
  	android:layout_height="wrap_content"  
    android:text="单选项" />
  
<RadioButton
    android:id="@+id/radioButton"
    android:layout_width="wrap_content"
  	android:layout_height="wrap_content"  
    android:text="单选按钮" />
</TableRow>
<AnalogClock
  	android:layout_width="wrap_content"
  	android:layout_height="wrap_content"  
    android:text="时钟" />
  
</TableLayout>

 产生的结果就是:



 通过结果大家和代码一分析便可知就是TableRow在作怪。就想到与table中row一行行的。其他的大体和线性的布局差不多。这里就不多说了。

在这里比较常用的代码解析

 

 

 android:collapseColumns="1"  

   表示隐藏该TableLayout里的TableRow的列1,若有多个需要隐藏,用逗号隔开

 

 android:stretchColumns="1"

   表示列1为可伸展的列,若有多个需要伸展,用逗号隔开

 

 android:shrinkColumns="0"

   表示设置列0为可收缩的列,当可收缩的列太宽,以至于让其他列不全显示,会向纵向延伸空间。

 

 android:background="@drawble/图片名"

   设置View的背景图片

 

三、RelativeLayout

 

次布局管理器实现的是一种策略,让容器的中控件以相对于容器或容器的另一个控件的形式配置。

如:

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
 
    <TextView  
    android:id="@+id/userName"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="用户名:"
    />
	<EditText
	android:id="@+id/nameText"
   	android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/userName"
    />       
     <TextView  
    android:id="@+id/pwd"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="密码:"
    android:layout_below="@id/nameText"
    />
	<EditText
	android:id="@+id/edit"
   	android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/pwd"
    />    
  
</RelativeLayout>

 运行后:


这个界面就是一个登录窗口,我们调用android:android:layout_below="@id/id名"标签就会在上一个标签的下方,这个功能在很多方面都得到了应用。除了

这个还有layout_above、layout_toRightOf、Layout _toLeftOf等。

 

常用的代码解析:

 

 android:layout_centerInParent="true"

   将当前控件放置于其父控件的横向和纵向的中央部分

   判断都是用true 和 false

 

 android:layout_below="@id/id名"

   将当前控件放置于id引用为id名的控件下方

   还有layout_above、layout_toLeftOf(左边)、layout_toRightOf(右边)

 

 android:layout_marginLeft="20px"

   在当前控件左边20像素额外的空间。

 

 

四、FrameLayout

 

这个布局主要是动态显示单一视图。但可以向其中填充很多项。将一个项设置为可见,而其余项设置为不可见。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
  <ImageView
   android:id="@+id/oneImag"
   android:src="@drawable/one"
   android:scaleType="fitCenter"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" />
   <ImageView
   android:id="@+id/twoImag"
   android:src="@drawable/two"
   android:scaleType="fitCenter"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" />
   <ImageView
   android:id="@+id/threeImag"
   android:src="@drawable/three"
   android:scaleType="fitCenter"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" 
   android:visibility="gone"
   />
</FrameLayout>
 

FrameLayout不会强制一次值像是一个控件。如果想局部中添加了许多控件,那么FrameLayout会简单的奖控件堆叠在一起,最后一个控件为与最顶端。这样可以建立一个而非常有趣得UI。

  • 大小: 28 KB
  • 大小: 37.8 KB
  • 大小: 17.1 KB
  • 大小: 34.2 KB
  • 大小: 29.6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics