布局文件

所有的布局文件都res/layout下面,你可以选择新建xml文件来新建布局。点击进入编辑。在下方有两个可以切换的地方,点击graphical Layout可以看到效果,单击xml可以看到编辑的文件

这里以原始的helloword为例



 <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/container"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context="com.example.helloworld.MainActivity"
      >
      <TextView
          android:id="+id/tv_helloword"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/hello_world" />
  </LinearLayout>

在写布局文件之前要理解一个事,布局设置的属性是非常合理的,和一个人要画一幅画的过程是一样的,不要觉得属性设置的很奇怪。

首先看最外层的LinearLayout.忽略xmlns这些和命名空间有关的东东,只看布局相关。LinearLayout是线性布局,顾名思义就是要线一样来排列,类似的有RelativeLayout(相对布局),FrameLayout(帧布局),GridLayout(网格布局),AbsoluteLayout(绝对布局)等,这些都是类似壳子一样的东西,它们决定了里面包裹的控件是怎么排列的。

无论是layout还是TextView,Button这样的控件,要显示在屏幕上,肯定是要知道它的大小的,而android控件的大小是与其父母紧密相关,当然也可以指定具体的数值。android:layout_width 是布局的宽度,android:layout_height是布局的高度。不指定具体的数值会有三个选项供选择fill_parent,match_parent,wrap_content,其中fill_parent和match_parent都是一样的效果,即最大的化的填充父母布局。fill_parent是比较老的参数,如果要填充父母布局,一般就选择match_parent,而wrap_parent则是自适应父母,由系统来决定你的控件相对于父母布局有多大。

既然是设置大小,肯定是可以指定数值的,比如100px,100dp等,单位不同,带来的效果不同,dp单位叫设备无关像素,px单位就是像素单位,一个Px就是一个像素,多数情况下选择dp,因为Android手机的屏幕大小是很不一致的,如果选择px为单位会使得视图的效果感很差,关于什么叫dp,px如果要细细说明又可以写上一篇,这里就暂时跳过。

知道了大小,有时候需要引用到这个控件或者布局,这时候就得知道它的唯一标识。设置唯一标识的方式为android:id="+id/xxxx",后面的xxxx为自定义的名字,给这个布局或者控件命名。如果根本引用不到这个控件或者布局就没有定义标识的必要。

理论上除了布局的大小是一定要有的,其它的都是可选的。根据实际情况,linearLayout既然是线程布局,就可以横着排列,或者竖着排列。LinearLayout由 android:orientation来设置是何种方式,两个选择,一个是"vertical",一个是“horizontal”默认情况是horizontal.看代码和效果

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.helloworld.MainActivity"
    android:orientation="horizontal"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    </LinearLayout>

效果:

以上是水平是,垂直的代码部分就只是android:orientation = "horizontal"

效果图:

时间: 2024-07-30 02:07:19

布局文件的相关文章

对另一个布局文件里的Button按钮进行监听

布局文件里面的Button写上 onClick = “onClick”,然后在你当前Activity的onClick方法中根据Button的id来做相应的操作 android:id="@+id/single_file_download" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" androi

【Android】Android Layout Binder——根据layout布局文件自动生成findViewById的java代码的神器

Android Layout Binder是一个网站,能够在线的根据layout布局文件自动生成findViewById的java代码. 网址是http://android.lineten.net/layout.php 如图:

Android布局文件经验

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

Android 多个界面复用一个布局文件

1.layout_common.xml 复用的布局文件 <?xml version="1.0" encoding="utf-8"?> <!-- 复用的布局文件 --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android

eclipse创建android项目无法正常预览布局文件

eclipse创建android项目时,预览layout.xml文件时提示: This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in,导致无法正常预览布局文件.问题根源:SDK版本过高,ADT版本过低.解决方法如下. 工具/原料 eclipse ADT插件 方法/步骤 1 找到eclipse文件夹,打开eclipse软件,创建a

Android的布局文件遇到Exception raised during rendering: java.lang.System.arraycopy([CI[CII)V问题

打开xml的布局文件,发现布局无法显示预览,而且报错:Exception raised during rendering: java.lang.System.arraycopy([CI[CII)V.原来是因为用了最新的API 20.这个是Android用于开发可穿戴设备的,不支持EditText.将API改为20之前的就行了.

获取联系人【自己定义布局文件与主布局文件相连,数据库内容查找并显示】

一.自己定义布局文件list_item_users.xml <RelativeLayout 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_

自定义ListView适配器Adapter引用布局文件的情况下实现点击列表项时背景颜色为灰色

listview控件设置适配器的时候,如果使用自定义的adapter,比如MyArrayAdapter extends ArrayAdapter<String> 如果listitem布局文件不做任何处理的话,点击列表项的时候不会出现背景色灰色的现象. 实现思路是:在listitem布局文件中为该布局文件中的父控件:LinearLayout,设置android:background="@drawable/listitemclickseletor" 其中listitemclic

使用布局文件(Layout)

布局文件类似于web form中的母版页,定义一个布局文件,然后在需要使用的视图中指定使用的布局文件,视图会把当前的动态绑定的数据传递到布局文件中,布局文件中也可以访问到这些数据.渲染视图文件中的数据在布局文件中必须使用@RenderBody()方法进行渲染,如果要使用视图文件中的数据就可以使用:@ViewBag.Title来访问视图文件中的数据. Index.cshtml @{ Layout = "~/Views/Shared/_SiteLayout.cshtml";//声明使用的布

Android学习---ListView和Inflater的使用,将一个布局文件转化为一个对象

本文将介绍ListView和Inflater的使用,将接上一篇文章内容. 一.什么是ListView? 在android开发中ListView是比较常用的控件,ListView 控件可使用四种不同视图显示项目,1.大(标准)图标2.小图标3.列表4.报表,比较常用的是列表的形式.ListItem 对象可包含文本和图片.然而,若要使用图片则必须通过 Icons 和 SmallIcons 属性引用 ImageList控件. 本文将接着上一篇文章,将sqlite数据库的数据以列表的形式显示出来. 二.