Android 布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".AndroidTableLayoutActivity" >

    <!-- 定义第一个表格,指定第2列允许收缩,第3列允许拉伸 -->

    <TableLayout
        android:id="@+id/tablelayout01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:shrinkColumns="1"
        android:stretchColumns="2" >

        <!-- 直接添加按钮,自己占用一行 -->

        <Button
            android:id="@+id/btn01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="独自一行" >
        </Button>

        <TableRow>

            <Button
                android:id="@+id/btn02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="普通" >
            </Button>

            <Button
                android:id="@+id/btn03"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="允许被收缩允许被收缩允许被收缩允许被收缩" >
            </Button>

            <Button
                android:id="@+id/btn04"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="允许被拉伸" >
            </Button>
        </TableRow>
    </TableLayout>
    <!-- 定义第2个表格,指定第2列隐藏 -->

    <TableLayout
        android:id="@+id/tablelayout02"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:collapseColumns="1" >

        <TableRow>

            <Button
                android:id="@+id/btn05"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="普通" >
            </Button>

            <Button
                android:id="@+id/btn06"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="被隐藏列" >
            </Button>

            <Button
                android:id="@+id/btn07"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="允许被拉伸" >
            </Button>
        </TableRow>
    </TableLayout>
    <!-- 定义第3个表格,指定第2列填满空白-->

    <TableLayout
        android:id="@+id/tablelayout03"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="1"
         >

        <TableRow>

            <Button
                android:id="@+id/btn08"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="普通" >
            </Button>

            <Button
                android:id="@+id/btn09"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="填满剩余空白" >
            </Button>
        </TableRow>
    </TableLayout>
    <!-- 定义第3个表格,指定第2列横跨2列-->

    <TableLayout
        android:id="@+id/tablelayout04"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         >

        <TableRow>

            <Button
                android:id="@+id/btn10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="普通" >
            </Button>

            <Button
                android:id="@+id/btn11"
                android:layout_column="2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="填满剩余空白" >
            </Button>
        </TableRow>
    </TableLayout>
</LinearLayout>

1、登录

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:stretchColumns="0,3">
    <TableRow>
        <TextView />
        <TextView
            android:text="账   号:"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24px"
            />
        <EditText
            android:id="@+id/account"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24px"
            android:minWidth="220px"/>
        <TextView />
    </TableRow>
    <TableRow android:layout_marginTop="20px">
        <TextView />
        <TextView
            android:text="密  码:"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <EditText
            android:id="@+id/pwd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="220px"
            android:textSize="24px"
            android:inputType="textPassword"/>
        <TextView />
    </TableRow>
    <TableRow android:layout_marginTop="20px">
        <TextView />
        <Button
            android:id="@+id/login"
            android:text="登录"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <Button
            android:id="@+id/quit"
            android:text="退出"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView />
    </TableRow>
</TableLayout>

2、LinearLayout(线性布局)

要点:

android:orientation="vertical"垂直线性布局,"horizontal"水平线性布局

android:gravity="top"(buttom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical、clip_horizontal)控制布局中控件的对齐方式。如果是没有子控件的控件设置此属性,表示其内容的对齐方式,比如说TextView里面文字的对齐方式;若是有子控件的控件设置此属性,则表示其子控件的对齐方式,gravity如果需要设置多个属性值,需要使用“|”进行组合

android:gravity 与 android:layout_gravity的区别 android:gravity是指定本元素的子元素相对它的对齐方式。 android:layout_gravity是指定本元素相对它的父元素的对齐方式。

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

例:

布局代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".LinearLayoutActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#aa0000"
            android:gravity="center_horizontal|center_vertical"
            android:text="第一列"
            android:textSize="15sp" >
        </Button>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#00aa00"
            android:gravity="center_horizontal"
            android:text="第二列"
            android:textSize="15sp" >
        </Button>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#0000aa"
            android:gravity="center|bottom"
            android:text="第三列"
            android:textSize="15sp" >
        </Button>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#aaaa00"
            android:gravity="bottom"
            android:text="第四列"
            android:textSize="15sp" >
        </Button>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="bottom"
            android:text="第1行"
            android:textSize="15sp" >
        </Button>

        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="bottom"
            android:text="第2行"
            android:textSize="15sp" >
        </Button>

        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="bottom"
            android:text="第3行"
            android:textSize="15sp" >
        </Button>

        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="bottom"
            android:text="第4行"
            android:textSize="15sp" >
        </Button>
    </LinearLayout>

</LinearLayout>
时间: 2024-08-23 19:36:05

Android 布局文件的相关文章

Android布局文件经验

1.父控件中含有多个子控件时,往往遵循长子优先的原则,即长子如果很大可能占满父空间,使次子们出局: 2.假设TableLayout有2行,其中一行未设定列间长度比例,而另一行设定了,则未设定行可能也会遵循设定行的列间长度比例: 3.在某个区域(如TableLayout中某个单元格)显示某张超大的图片,希望图片总是自适应单元格而不是把单元格撑爆.解决方案:将单元格放在LinearLayout中,给LinearLayout设置android:layout_width="wrap_content&qu

【Android】Android布局文件的一些属性值

第一类:属性值 true或者 false android:layout_centerHrizontal 水平居中android:layout_centerVertical 垂直居中android:layout_centerInparent 相对于父元素完全居中android:layout_alignParentBottom 贴紧父元素的下边缘android:layout_alignParentLeft 贴紧父元素的左边缘android:layout_alignParentRight 贴紧父元素的

Android布局文件的加载过程分析:Activity.setContentView()源码分析

大家都知道在Activity的onCreate()中调用Activity.setContent()方法可以加载布局文件以设置该Activity的显示界面.本文将从setContentView()的源码谈起,分析布局文件加载所涉及到的调用链.本文所用的源码为android-19. Step 1  .Activity.setContentView(intresId) public void setContentView(int layoutResID) { getWindow().setConten

Android布局文件中xml里的xmlns:的作用

一些新手经常看到布局文件中都有xmlns:android="http://schemas.android.com/apk/res/android"或者 xmlns:tools=http://schemas.android.com/tools 却不知道是什么意思. 其实它是告诉Android开发工具你准备使用Android命名空间里的一些通用属性.在所有Android XML设计文件中最外层的标记必须使用这个树形. 它可以提示你输入什么,不该输入什么,什么是对的,什么是错的,也可以理解为

Android布局文件夹引起的问题

Android 运行到setContentView(R.layout.splash); 总是出现如下的错误: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.esri.localtiledlayer/com.login.SplashScreen}: android.view.InflateException: Binary XML file line #17: Error inflating class

android 布局文件中xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;

http://blog.163.com/benben_long/blog/static/199458243201411394624170/ xmlns:android="http://schemas.android.com/apk/res/android的作用是 这个是xml的命名空间,有了他,你就可以alt+/作为提示,提示你输入什么,不该输入什么,什么是对的,什么是错的,也可以理解为语法文件.或者语法判断器什么的 这个主要作用是在运行的时候那些控件的属性都是通过它来识别的,如果上面你写错了,

Android 布局文件中的values-v11,values-v14,values-v19代表的含义

对于初学者而言,有时候看到项目中会有values-v11,values-v14,values-v19等文件夹,里面有dimens,styles等.那具体代表是什么呢?我从2.3时代开始接触android,突然有一天到了4.0以上系统,我发现多了v14等文件夹,之后才明白跟android 的api版本有关系. values-v11代表在API 11+的设备上,用该目录下的styles.xml代替res/values/styles.xml values-v14代表在API 14+的设备上,用该目录下

Android布局文件layout.xml的一些属性值

第一类:属性值 true或者 false android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 android:layout_centerInparent 相对于父元素完全居中 android:layout_alignParentBottom 贴紧父元素的下边缘 android:layout_alignParentLeft 贴紧父元素的左边缘 android:layout_alignParentRight 贴

android 布局文件 ScrollView 中的 listView item 显示不全解决方案

import android.content.Context;import android.util.AttributeSet;import android.widget.ListView; /** * Created by wanqi on 15-7-20. */public class ScrollListView extends ListView { public ScrollListView(Context context, AttributeSet attrs) { super(con