Android进阶(二十七)Android原生扰人烦的布局

Android原生扰人烦的布局

在开发Android应用时,UI布局是一件令人烦恼的事情。下面主要讲解一下Android中的界面布局。

一、线性布局(LinearLayout)

线性布局分为:

(1)垂直线性布局;

(2)水平线性布局;

针对这两种区别,只是一个属性的区别

<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:orientation="horizontal" 即可。

二、 相对布局(RelativeLayout)

一般线性布局满足不了们实际项目中的需要,就是一般做Web界面的UI设计一样,也是存在相对元素的一些CSS样式的布局。RelativeLayout参数有:Width,Height,Below,AlignTop,ToLeft,Padding,和MerginLeft。

关键源码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="请输入:"/>
    <EditText
        android:id="@+id/entry"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background"
        android:layout_below="@id/label"/>
    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/entry"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10dip"
        android:text="确定" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/ok"
        android:layout_alignTop="@id/ok"
        android:text="取消" />
</RelativeLayout>  

其中,android:layout_below=”@id/label”设置了EditText处于TextView下方;在Button中android:layout_below=”@id/entry”设置该Button位于EditText下。

实例效果:

三、表单布局(TableLayout)

TableLayout由许多TableRow组成,每个TableRow都会定义一个Row。TableLayout容器不会显示Row,Column或Cell的边框线,每个Row拥有0个或多个Cell,每个Cell拥有一个View对象。表格由行和列组成许多单元个,允许单元格为空。但是单元格不能跨列,这与Html不同。

<View
    android:layout_height="2dip"
    android:background="#FF909090" />
<TableRow>
    <TextView
        android:text="*"
        android:padding="3dip" />
    <TextView
        android:text="导入..."
        android:padding="3dip" />
</TableRow>
<TableRow>
    <TextView
        android:text="*"
        android:padding="3dip" />
    <TextView
        android:text="导出..."
        android:padding="3dip" />
    <TextView
        android:text="Ctrl-E"
        android:gravity="right"
        android:padding="3dip" />
</TableRow>  

实例效果:

android:gravity="center" 书面解释是权重比。其时就是让它居中显示。

android:stretchColumns="1,2,3,4" 它的意思就是自动拉伸1,2,3,4列。

若需实现组件居中显示,布局应如下:

          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center_vertical|center_horizontal"

TableRow平分列

每一列的宽度设置为android:layout_width="0.0dip",在设置每一列的android:layout_weight=“1”

因为ayout_weight是将剩余空间按权重分配,而不是将全部空间按权重分配。

代码如下:

<TableRow
    android:paddingTop="15px">
    <Button
        android:id="@+id/Register"
        android:text="@string/register"
        android:layout_width="0.0dip"
        android:layout_weight="1"
        android:onClick="register"/>
    <Button
        android:id="@+id/cancel"
        android:layout_width="0.0dip"
        android:layout_weight="1"
        android:text="@string/button_cancel" />
    </TableRow>

视图如下:

四、切换卡(TabWidget)

切换卡经常用在一下选项上,类似于电话簿界面,通过多个标签切换显示不同内容。而其中,TabHost是一个用来存放Tab标签的容器,通过getTabHost方法来获取TabHost的对象,通过addTab方法向容器里添加Tab。Tab在切换时都会产生一个事件,可以通过TabActivity的事件监听setOnTabChangedListener.

【扩展点】TabHost

类概述

提供选项卡(Tab页)的窗口视图容器。此对象包含两个子对象:一组是用户可以选择指定Tab页的标签;另一组是FrameLayout用来显示该Tab页的内容。个别元素通常控制使用这个容器对象,而不是设置在子元素本身的值。

(译者madgoat注:即使使用的是单个元素,也最好把它放到容器对象ViewGroup里)

内部类

interface TabHost.OnTabChangeListener

接口定义了当选项卡更改时被调用的回调函数

interface TabHost.TabContentFactory

当某一选项卡被选中时生成选项卡的内容

class TabHost.TabSpec

单独的选项卡,每个选项卡都有一个选项卡指示符,内容和tag标签,以便于记录.。

关键源码

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <TextView
                android:id="@+id/textview1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="this is a tab" />
            <TextView
                android:id="@+id/textview2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="this is another tab" />
            <TextView
                android:id="@+id/textview3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="this is a third tab" />
        </FrameLayout>
    </LinearLayout>
</TabHost> 

     处理类

//声明TabHost对象
    TabHost mTabHost;
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //取得TabHost对象
        mTabHost = getTabHost();
        /* 为TabHost添加标签 */
        //新建一个newTabSpec(newTabSpec)
        //设置其标签和图标(setIndicator)
        //设置内容(setContent)
        mTabHost.addTab(mTabHost.newTabSpec("tab_test1")
                .setIndicator("TAB 1",getResources().getDrawable(R.drawable.img1))
                .setContent(R.id.textview1));
        mTabHost.addTab(mTabHost.newTabSpec("tab_test2")
                .setIndicator("TAB 2",getResources().getDrawable(R.drawable.img2))
                .setContent(R.id.textview2));
        mTabHost.addTab(mTabHost.newTabSpec("tab_test3")
                .setIndicator("TAB 3",getResources().getDrawable(R.drawable.img3))
                .setContent(R.id.textview3));
        //设置TabHost的背景颜色
        mTabHost.setBackgroundColor(Color.argb(150, 22, 70, 150));
        //设置TabHost的背景图片资源
        //mTabHost.setBackgroundResource(R.drawable.bg0);
        //设置当前显示哪一个标签,默认下标从0开始
        mTabHost.setCurrentTab(0);
        //标签切换事件处理,setOnTabChangedListener
        mTabHost.setOnTabChangedListener(new OnTabChangeListener(){
            public void onTabChanged(String tabId) {
                Dialog dialog = new AlertDialog.Builder(Examples_04_29Activity.this)
                        .setTitle("提示")
                        .setMessage("当前选中:"+tabId+"标签")
                        .setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton){
                                dialog.cancel();
                            }
                        }).create();//创建按钮
                dialog.show();
            }
        });
    }  

实例效果:

参考文献

1.http://blog.csdn.net/fancylovejava/article/details/45787729/

美文美图

时间: 2024-08-08 01:29:37

Android进阶(二十七)Android原生扰人烦的布局的相关文章

Android进阶(二十三)Android开发过程之实例讲解

Android开发过程之实例讲解 前言 回过头来审视之前做过的Android项目,发觉自己重新开发时忽然间不知所措了,间隔了太长时间没有开发导致自己的Android技能知识急剧下降.温故而知新. 废话少说,进入正题~ 下面主要以自己之前开发过的Android小项目为例,探讨Android开发基本流程,以及其中所涉及到的原理. 项目名称为"我查查",主要的实现功能是查询.添加商品评价,分享购物体验. 主要界面如下: 图1 主功能界面 图2 查看商品信息 图3 添加新评论 图4 扫码操作

Android进阶(二十八)上下文菜单ContextMenu使用案例

上下文菜单ContextMenu使用案例 前言 回顾之前的应用程序,发现之前创建的选项菜单无法显示了.按照正常逻辑来说,左图中在"商品信息"一栏中应该存在选项菜单,用户可进行分享等操作,但是现在此操作莫名其妙的消失了.写了个测试Demo,如中图所示,一切按照逻辑显示正常.怪就怪在项目中无法显示,起初设想是因为Android系统版本太高问题,但是在别的手机上测试之后发现问题依旧存在.难道是因为顶部Tab标题栏遮挡住了选项菜单的显示?继续测试,通过在别的没有Tab标题栏的页面测试选项菜单,

我的Android进阶之旅------&gt;Android疯狂连连看游戏的实现之实现游戏逻辑(五)

在上一篇<我的Android进阶之旅------>Android疯狂连连看游戏的实现之加载界面图片和实现游戏Activity(四)>中提到的两个类: GameConf:负责管理游戏的初始化设置信息. GameService:负责游戏的逻辑实现. 其中GameConf的代码如下:cn\oyp\link\utils\GameConf.java package cn.oyp.link.utils; import android.content.Context; /** * 保存游戏配置的对象

我的Android进阶之旅------&gt; Android在TextView中显示图片方法

面试题:请说出Android SDK支持哪些方式显示富文本信息(不同颜色.大小.并包含图像的文本信息),并简要说明实现方法. 答案:Android SDK支持如下显示富文本信息的方式. 1.使用TextView组件可以显示富文本信息.在TextView组件中可以使用富文本标签来显示富文本信息,这种标签类似于HTML标签,但比HTML标签简单,支持有限的几种显示富文本的方式.如<font>标签用于设置字体和颜色,<b>用于设置粗体.包含这些标签的文本不能直接作为TextView.se

我的Android进阶之旅------&gt;Android利用Sensor(传感器)实现水平仪功能的小例

这里介绍的水平仪,指的是比较传统的气泡水平仪,在一个透明圆盘内充满液体,液体中留有一个气泡,当一端翘起时,该气泡就会浮向翘起的一端. 利用方向传感器返回的第一个参数,实现了一个指南针小应用. 我的Android进阶之旅------>Android利用Sensor(传感器)实现指南针功能 (地址:http://blog.csdn.net/ouyang_peng/article/details/8801204) 接下来,我们利用返回的第二.三个参数实现该水平仪.因为第二个参数,反映底部翘起的角度(当

我的Android进阶之旅------&gt; Android为TextView组件中显示的文本添加背景色

通过上一篇文章 我的Android进阶之旅------> Android在TextView中显示图片方法 (地址:http://blog.csdn.net/ouyang_peng/article/details/46916963) 我们学会了在TextView中显示图片的方法,现在我们来学习如何为TextView组件中显示的文本添加背景色.要求完成的样子如图所示: 首先来学习使用BackgroundColorSpan对象设置文字背景色,代码如下: TextView textView=(TextV

Android笔记二十七.Bitmap之简易图片查看器

转载请表明出处:http://blog.csdn.net/u012637501(嵌入式_小J的天空) 为了增强用户之间的交互,Android系统中提供了一些API和部件给我们开发美观有趣的应用.比如Android系统提供了ImageView来显示静态图片.AnimationDrawble来开发逐帧动画以及通过Animation对普通图片使用不减动画等.另外,Android应用中的图片不仅包括*.png.*.jpg.*.gif等格式的位图,也包括使用XML资源文件定义的各种Drawable对象.关

Android进阶(二十二)设置TextView文字水平垂直居中

设置TextView文字水平垂直居中 有2种方法可以设置TextView文字居中: 一:在xml文件设置:android:gravity="center" 二:在程序中设置:m_TxtTitle.setGravity(Gravity.CENTER); 备注:android:gravity和android:layout_gravity的区别在于前者对控件内部操作,后者是对整个控件操作. 例如:android:gravity="center"是对textView中文字居

进阶二之Android重力感应(二)

路途再远也要一步步的走不止为了生存而是对其的热爱 本讲内容:Android重力感应 一.SensorMannager传感器管理对象 1.取得SensorMannager 手机中的所有传感器都须要通过SensorMannager来访问,调用getSystemService(SENSOR_SERVICE)方法就可以拿到当前手机的传感器管理对象. sm = (SensorManager) getSystemService(SENSOR_SERVICE); 2.取得Sensor目标各类的值(getSen