安卓

重拾安卓(引言与布局)

2016-01-24  本文已影响301人  Stark_Dylan

大概已经有3年没有碰过安卓了,第一次接触Android开发还是在大二的时候,现在已经毕业半年了、当时学习的也仅仅是Android开发的基础。 最近,我闲时的作品「微商下线助手」上线了,但是通过接触一部分用户,使用Android的人还是蛮多的,所以不得不为我自己的这个iOS的应用做一个Android的版本。
过了3年,技术发生了很多变化,记得当时使用的还是Eclipse和netbeans,现在已经有了从某软件改来的Android Studio了,还有Gradle,Maven这些类似于cocoapods的管理依赖的工具。而且现在对安卓的布局很不熟悉,只是知道一些Java的语法。
重拾安卓是一个漫长的过程,但是我的时间还比较的紧凑,所以今天开始重拾安卓,也会在学习的过程中把自己的一些经历经验分享出来。 一些iOS的开发者也可以把这当作是iOS跳槽Android的一个借鉴经验。

我决定首先从布局开始,在我看来,像控件这些东西慢慢学就行,布局很重要。至于Java语法,在学习中大家慢慢熟悉。还有上边所说的一些依赖管理工具,自行百度。

布局

Android中常用的5大布局方式有以下几种:

知道了基本概念之后,我们开始动手试一下效果。

首先打开Android Studio创建一个新的项目, 全部默认就行。然后在activity_main.xml 中直接尝试一下线性布局。

6291FCB7-95DB-48DB-BCB2-A19A5768CCC9.png

删掉原有的RelativeLayout标签

Demo:

<?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" >    

 <LinearLayout       
    android:layout_width="fill_parent"        
    android:layout_height="wrap_content"        
    android:orientation="vertical" >        
        <EditText           
            android:layout_width="fill_parent"            
           android:layout_height="wrap_content"            
        />    
</LinearLayout>        

<LinearLayout        
    android:layout_width="fill_parent"        
    android:layout_height="wrap_content"        
    android:orientation="horizontal"        
    android:gravity="right" >        
    <!-- android:gravity="right"表示Button组件向右对齐 -->       
    <Button           
        android:layout_height="wrap_content"            
        android:layout_width="wrap_content"           
        android:text="确定" />       
    <Button            
        android:layout_height="wrap_content"     
        android:layout_width="wrap_content"            
       android:text="取消" />    
</LinearLayout>    

</LinearLayout> 

看下效果

A1D35F53-60DB-412C-9E0E-67E84BDEADC1.png

我们来解释一下这段xml

首先,最外层的LinearLayout 当前界面的基本布局方式,大致的规定了当前的方向为水平的,宽、高都是填满父视图。

<LinearLayout       
    android:layout_width="fill_parent"        
    android:layout_height="wrap_content"        
    android:orientation="vertical" >        
        <EditText           
            android:layout_width="fill_parent"            
           android:layout_height="wrap_content"            
        />    
</LinearLayout>        

这段,也就是第一个元素,宽度是充满父视图,wrap_content意思是自适应大小,强制性地使视图扩展以便显示其全部内容。以TextViewImageView控件为例,设置为wrap_content将完整显示其内部的文本和图像。布局元素将根据内容更改大小。fill_parent就像是UIViewModeScaleAspectFill一样,强制的让它充满。

<LinearLayout        
    android:layout_width="fill_parent"        
    android:layout_height="wrap_content"        
    android:orientation="horizontal"        
    android:gravity="right" >        
    <!-- android:gravity="right"表示Button组件向右对齐 -->       
    <Button           
        android:layout_height="wrap_content"            
        android:layout_width="wrap_content"           
        android:text="确定" />       
    <Button            
        android:layout_height="wrap_content"     
        android:layout_width="wrap_content"            
       android:text="取消" />    
</LinearLayout>

以上代码就是下边的按钮,这两个按钮也有一个布局,外层的Layout为线性横向布局,所以这两个按钮是在一排,如果是纵向的话,可想而知.

C22FE8E0-995C-4E37-86A7-5489F8A25427.png

至此,大家应该大致了解了现形布局,很简单,只有两种方式,横向,纵向。

所以我们来练习一下,需求是这样的,横向的3个View平均分当前视图,高度为屏幕一半,接着下边有3个纵向的视图,平分视图余下的部分。原型图是这样的:

BC5526B6-44FA-4C2C-AE37-5DC83548267B.png

这里建议大家想一下,这里大家会想,怎么才能平分呢?layout_weight!

layout_weight属性以控制各个控件在布局中的相对大小。layout_weight属性是一个非负整数值。线性布局会根据该控件layout_weight值与其所处布局中所有控件layout_weight值之和的比值为该控件分配占用的区域。 例如,在水平布局的LinearLayout中有两个Button,这两个Button的layout_weight属性值都为1, 那么这两个按钮都会被拉伸到整个屏幕宽度的一半。如果layout_weight指为0,控件会按原大小显示,不会被拉伸; 对于其余layout_weight属性值大于0的控件,系统将会减去layout_weight属性值为0的控件的宽度或者高度, 再用剩余的宽度或高度按相应的比例来分配每一个控件显示的宽度或高度。

了解了这个之后,应该很容易了把。我们首先把当前屏幕分成上下平分的2部分,


<LinearLayout    
 android:layout_width="fill_parent"   
 android:layout_height="fill_parent"    
android:layout_weight="1"    ></LinearLayout>

<LinearLayout    
android:layout_width="fill_parent"    
android:layout_height="fill_parent"    
android:layout_weight="1"    >
</LinearLayout>

这样,通过layout_weight属性,平分屏幕,接下来的操作,大家自己去写吧。

1F435AAD-8549-4AB9-917D-19A9146B0BEB.png

帧布局是从屏幕的左上角(0,0)坐标开始布局,多个组件层叠排列,第一个添加的组件放到最底层,最后添加到框架中的视图显示在最上面。上一层的会覆盖下一层的控件。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    
android:layout_width="fill_parent"  
android:layout_height="fill_parent">    

<View        
android:layout_width="300dp"        
android:layout_height="300dp"        
android:background="#00BFFF"        
/>    
<View        
android:layout_width="260dp"        
android:layout_height="260dp"        
android:background="#FFC0CB"        
/>    
<View        
android:layout_width="220dp"        
android:layout_height="220dp"        
android:background="#0000FF"        
/>
</FrameLayout>
DFF5C078-B822-4E8F-8249-76F4CE095ADA.png
在这里出现了宽度是220dp, dp很神奇,大家看一下。
神器的DP

表格布局是一个ViewGroup以表格显示它的子视图(view)元素,即行和列标识一个视图的位置。
表格布局常用的属性如下:
android:collapseColumns:隐藏指定的列
android:shrinkColumns:收缩指定的列以适合屏幕,不会挤出屏幕
android:stretchColumns:尽量把指定的列填充空白部分android:layout_column:控件放在指定的列
android:layout_span:该控件所跨越的列数

<?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" 
    > 
    <TableRow> 
        <Button 
            android:text="Button1" 
            /> 
        <Button 
            android:text="Button2" 
            /> 
        <Button 
            android:text="Button3" 
            /> 
    </TableRow> 
    <TableRow> 
        <Button 
            android:text="Button4" 
            /> 
        <Button 
            android:layout_span="2" 
            android:text="Button5" 
            /> 
    </TableRow> 
      
</TableLayout> 
43A24BC2-B9D4-4522-A836-05B415DC61A0.png

相对布局是按照组件之间的相对位置来布局,比如在某个组件的左边,右边,上面和下面等。 相对布局常用属性请参考博客。在iOS中,多用相对布局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:padding="10px" 
    > 
    <TextView    
        android:id="@+id/tev1" 
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"   
        android:layout_marginBottom="30dp" 
        android:text="Please Type Here:" 
        /> 
    <EditText 
        android:id="@+id/tx1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/tev1" 
        /> 
    <Button 
        android:id="@+id/btn1" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:layout_below="@id/tx1" 
        android:layout_alignParentRight="true" 
        android:text="确定" 
        /> 
    <Button 
        android:id="@+id/btn2" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:layout_below="@id/tx1" 
        android:layout_toLeftOf="@id/btn1" 
        android:layout_marginRight="30dp" 
        android:text="取消" 
        /> 
</RelativeLayout> 

关于其中出现的padding,margin等属性,在边的链接中都可以找的到,我不做过多的描述喽。都是一些简单的属性。

绝对布局通过指定子组件的确切X,Y坐标来确定组件的位置,在Android2.0 API文档中标明该类已经过期,可以使用FrameLayout或者RelativeLayout来代替。所以这里不再详细介绍。

学习自 IT的点点滴滴
CopyRight@ Dylan. 2015-1-24

上一篇 下一篇

猜你喜欢

热点阅读