zoukankan      html  css  js  c++  java
  • Android_Layout_xml布局

    1.构建xml布局文件

    使用android提供的xml布局,可以快速构建UI界面,每个xml文件必须包含一个根元素,该xml文件位于res/layout/目录中。例如:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello, I am a TextView" />
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button" />
    </LinearLayout>

    2.加载xml布局文件

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	//加载xml布局文件应该位于首行
    	setContentView(R.layout.activity_main);
    }

    3.属性ID

    (1).xml中添加ID属性

    android:id="@+id/button"

    (2).xml中引用ID

    android:id="@id/button"

    (3).得到控件对象

    Button myButton = (Button) findViewById(R.id.button);

    4.布局的宽与高属性

    所有视图都应该指定layout_width 与 layout_height属性,对该属性的描述主要有2个值

    wrap_content根据控件内容的大小来定义当前控件整体大小

    match_parent填满父类允许填满的空间位置,在 API Level 8之前使用fill_parent

    一般在指定宽高时,推荐使用dp,而不是pixels。因为在不同的尺寸屏幕手机上,dp有更好的展示效果.

    在LinearLayout布局中,假设定义了android:orientation="horizontal",且使用了android:layout_weight来表示子类控件的权重,此时子类控件推荐使用layout_width="0dp",子类控件将根据权重大小等比宽度;如果子类控件使用了layout_width="match_parent",将根据权重等比缩小。同样竖直方位也是如此!

    5.layout_gravity与gravity

    layout_gravity即设置当前视图相对于父类的位置摆放,gravity即控制当前视图的子类相对于父类的位置摆放。但有时layout_gravity不会起作用,原因很简单,就是忽略了父类的方位布局!

    比如父类布局设置了android:orientation="vertical",而其子类有个button按钮,设置了android:layout_gravity="center_vertical",不会有任何效果,因为button遵循竖直排布,但只有水平方向的移动才会有效果!

    因此,父类布局为vertical时,在设置layout_gravity时,只能控制水平方向的移动;在父类布局为horizontal时,在设置layout_gravity时,只能控制竖直方向的移动!而layout_gravity="center"即为可能有效果的那个方位居中!

    6.margin与padding

    margin主要控制当前视图相对于父类视图相间的位置距离大小

    padding,比如一个Button按钮,text="Bt";如果指定了android:paddingLeft="30dp",意为Bt距离当前控件的左边为30dp大小

    7.常见布局

    (1).LinearLayout

    LinearLayout为一个视图组,所有子类按照一单方向排列,必须指定android:orientation属性,该值为horizontal或者vertical,当使用子类控件使用权重layout_weight时,宽或者高推荐使用0dp

    (2).RelativeLayout

    RelativeLayout一个布局相对于另一个视图的位置,在上下左右或者相对于父类等等

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingLeft="16dp"
        android:paddingRight="16dp" >
    
        <EditText
            android:id="@+id/name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/reminder" />
    
        <Spinner
            android:id="@+id/dates"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@id/name"
            android:layout_toLeftOf="@+id/times" />
    
        <Spinner
            android:id="@id/times"
            android:layout_width="96dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@id/name" />
    
        <Button
            android:layout_width="96dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@id/times"
            android:text="@string/done" />
    
    </RelativeLayout>
  • 相关阅读:
    4.练习
    『Java面试题总结
    『Maven + Junit + Jacoco』单元测试覆盖率
    『Linux』命令
    『ElasticSearch』安装、健康值检查
    『类型转换』Object转Map、Map转Object
    『ElasticSearch』排序报错
    MD5加密解密网址总结
    汉字编码的理解
    ASCII码的理解
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3331442.html
Copyright © 2011-2022 走看看