main.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
<LinearLayout>
线性版面配置,在这个标签中,所有元件都是按由上到下的排队排成的
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- android:orientation="vertical" 表示竖直方式对齐
android:orientation="horizontal"表示水平方式对齐
android:layout_width="fill_parent"定义当前视图在屏幕上
可以消费的宽度,fill_parent即填充整个屏幕。
android:layout_height="wrap_content":随着文字栏位的不同
而改变这个视图的宽度或者高度。有点自动设置框度或者高度的意思
layout_weight 用于给一个线性布局中的诸多视图的重要度赋值。
所有的视图都有一个layout_weight值,默认为零,意思是需要显示
多大的视图就占据多大的屏幕空 间。若赋一个高于零的值,则将父视
图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight
值以及该值在当前屏幕布局的整体 layout_weight值和在其它视图屏幕布
局的layout_weight值中所占的比率而定。
举个例子:比如说我们在 水平方向上有一个文本标签和两个文本编辑元素。
该文本标签并无指定layout_weight值,所以它将占据需要提供的最少空间。
如果两个文本编辑元素每一个的layout_weight值都设置为1,则两者平分
在父视图布局剩余的宽度(因为我们声明这两者的重要度相等)。如果两个
文本编辑元素其中第一个的layout_weight值设置为1,而第二个的设置为2,
则剩余空间的三分之二分给第一个,三分之一分给第二个(数值越小,重要
度越高)。
-->
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="red"
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="green"
android:gravity="center_horizontal"
android:background="#00aa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="blue"
android:gravity="center_horizontal"
android:background="#0000aa"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="yellow"
android:gravity="center_horizontal"
android:background="#aaaa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2">
<TextView
android:text="row one"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row two"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row three"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row four"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
感觉这种形式有点像div+css的方式布局,不过这种方式的灵活性和div+css还是有些不及,主要是那android:layout_weight的值如何去确定,而且采用的是数值越小,重要度越高的方式,分配起来还得好好计算一下。
Java代码
Views.java
package com.cn.view;
import android.app.Activity;
import android.os.Bundle;
public class Views extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
分享到:
相关推荐
关于linearlayout布局案例
主要介绍了Android布局之LinearLayout自定义高亮背景的方法,实例分析了Android中LinearLayout布局参数设置技巧,需要的朋友可以参考下
LinearLayout是线性布局控件:要么横向排布,要么竖向排布,下面通过本篇文章给大家介绍Android布局之LinearLayout线性布局,涉及到android linearlayout 布局相关知识,对本文感兴趣的朋友一起学习吧
Android开发学习23】界面布局之线性布局LinearLayout代码
Android中线性布局LinearLayout的特点.pdf 学习资料 复习资料 教学资源
Android常见的布局类型是线性布局(LinearLayout),包含布局方向设置、基本使用、常用使用案例以及实现均分案例(垂直均分、水平均分)、以及线性布局嵌套,实现垂直且水平居中,整体按照“理论+案例”,分析清晰,...
Android 布局控件之LinearLayout详细介绍,需要的朋友可以参考一下
原需求,在一个伸缩列表中,自定义LinearLayout继承LinearLayout动态添加布局。 然而实现的时候:一共遍历了30条数据,却只显示了一条 断点查看代码:遍历addView()这个过程是正常的30次循环。那是布局的问题? ...
Android布局文件的属性值解析说明: 1 android:id [为控件指定相应的ID] 2 android:text [指定控件当中显示的文字 需要注意的是 这里尽量使用strings xml文件当中的字符串] 3 android:gravity [指定View组件的对齐...
安卓Android线性布局之计算器界面实现借鉴 一、Android线性布局(LinearLayout)简介 Android线性布局(LinearLayout)是一种常用的界面布局方式,它可以将控件按照水平或垂直方向排列。该布局方式可以使得界面...
android studio 初学者 Linearlayout 水平和垂直的布局文件
android 线性布局LinearLayout实例代码,需要的朋友可以参考一下
首先说的是LinearLayout布局下的居中一般是这样的: (注意:android:layout_width=”fill_parent” android:layout_height=”fill_parent” 属性中,若水平居中,至少在宽度上占全屏;若垂直居中,则在高度上占全屏...
Android 入门第二篇之 LinearLayout、AbsoluteLayout Android 的 UI 布局是基于容器的概念,Layout 作为容器,控件按照规定排列在其上面。这种布局方式与 JAVA 的 Swing 和 LWUIT 很像。控件和 Layout 之间有很多...
LinearLayout 线性布局 控制组件 横向 或者 纵向 排列 RelativeLayout 相对布局 子组件的位置总是相对兄弟组件,父容器来决定的 FrameLayout 帧布局、框架布局 创建一个空白区域, 一个区域成为一帧 TableLayout 表格...
线性布局(LinearLayout):按照垂直或者水平方向布局的组件。 帧布局(FrameLayout):组件从屏幕左上方布局组件。 表格布局(TableLayout):按照行列方式布局组件。 相对布局(RelativeLayout):相对其它组件的...
android不同的布局资源介绍,包括LinearLayout、RelativeLayout等