• 安卓开发中的4中基本布局


    1.LinearLayout(线性布局)

    (1)如果LinearLayout的排列方向是horizontal,内部的控件就绝对不能将宽度指定为match_parent,因为这样的话,单独一个控件会将整个水平方向占满,其他控件就没有可以放置的位置了。

    同理如果LinearLayout的排列方向是vertical,内部控件就不能将高度指定为match_parent

    (2)android:layout_gravity用于指定控件在在布局中的对齐方式(horizontal只能指定竖直方向上的对齐方式,vertical只能指定水平方向上的对齐方式)

        android:gravity用于指定文字在控件中的对齐方式

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"     <!--代表控件排列顺序,horizontal表示水平,vertical表示竖直-->
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
         <Button
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:id="@+id/button1"
             android:layout_gravity="top"
             android:text="button1" />
    
        <Button
            android:id="@+id/button2"
            android:layout_gravity="center"
            android:text="button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button3"
            android:layout_gravity="bottom"
            android:text="button3"/>
    </LinearLayout>

    (3)android:layout_weight

    将两个控件的宽度都指定为0,这时控件的宽度由layout_weight决定,这里表示EditText和Button将屏幕分成1:1的两部分

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <EditText
            android:id="@+id/input_message"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="Type something"
            />
        <Button
            android:id="@+id/send"
            android:layout_width="0dp"
            android:layout_height="wrap_content" 
            android:layout_weight="1"
            android:text="Send"
            />
    </LinearLayout>

    改进适配的Button写法

       <Button
            android:id="@+id/send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Send"
            />

    2.RelativeLayout相对布局

    (1)相对于父布局定位

    android:layout_alignParentLeft="true" 和父布局左对齐

    android:layout_centerInParent="true" 和父布局居中对齐

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
          <Button
              android:id="@+id/button1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:layout_alignParentTop="true"
              android:text="Button 1"/>
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="Button 2"/>
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="Button 3"/>
        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 4"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
             />
        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 5"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            />
    </RelativeLayout>

     (2)相对于控件定位

    android:layout_above="@+id/button3" 一个控件在另一个控件上方

    android:layout_toLeftOf="@+id/button3" 一个控件在另一个控件左侧

    android:layout_alignLeft="@+id/button3" 一个控件左边缘与另一个控件左边缘对齐

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
            <Button
                android:id="@+id/button3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="Button 3"/>
          <Button
              android:id="@+id/button1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_above="@+id/button3"
              android:layout_toLeftOf="@+id/button3"
              android:text="Button 1"/>
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/button3"
            android:layout_toRightOf="@+id/button3"
            android:text="Button 2"/>
    
        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 4"
            android:layout_below="@+id/button3"
            android:layout_toLeftOf="@+id/button3"
             />
        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 5"
            android:layout_below="@+id/button3"
            android:layout_toRightOf="@+id/button3"
            />
    </RelativeLayout>

    3.FrameLayout帧布局

    所有控件都默认摆在左上角,可以使用 layout_gravity属性来指定控件在布局中的对齐方式

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
          <TextView
              android:id="@+id/text_view"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="This is TextView"
              android:layout_gravity="left"/>
        <ImageView
            android:id="@+id/image_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"
            android:layout_gravity="right"/>
    </FrameLayout>

    4.百分比布局

    百分比布局是对相对布局和帧布局的功能扩展

    百分百布局定义在了support库当中,需要在项目的build.gradle中添加百分比库的依赖

    打开app/build.gradle文件,在dependencies闭包中添加以下内容

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
            exclude group: 'com.android.support', module: 'support-annotations'
        })
        compile 'com.android.support:appcompat-v7:25.2.0'
        compile 'com.android.support:percent:25.2.0' //加这一句
        compile 'com.android.support.constraint:constraint-layout:1.0.2'
        testCompile 'junit:junit:4.12'
    }
    <android.support.percent.PercentFrameLayout  
        xmlns:android="http://schemas.android.com/apk/res/android"  
        xmlns:app="http://schemas.android.com/apk/res-autop"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:id="@+id/button1"
            android:text="Button 1"
            android:layout_gravity="left|top"
            app:layout_widthPercent="50%"
            app:layout_heightPercent="50%"
            />
        <Button
            android:id="@+id/button2"
            android:text="Button 2"
            android:layout_gravity="right|top"
            app:layout_widthPercent="50%"
            app:layout_heightPercent="50%"
            />
        <Button
            android:id="@+id/button3"
            android:text="Button 3"
            android:layout_gravity="left|bottom"
            app:layout_widthPercent="50%"
            app:layout_heightPercent="50%"
            />
        <Button
            android:id="@+id/button4"
            android:text="Button 4"
            android:layout_gravity="right|bottom"
            app:layout_widthPercent="50%"
            app:layout_heightPercent="50%"
            />
    </android.support.percent.PercentFrameLayout>
  • 相关阅读:
    windows版本nginx帮助文档
    angular渲染超链接bug
    ionic踩过的一些坑
    ionic项目中使用自定义图标打包出现错误-broken import
    ionic项目中使用自定义图标
    ionic3运行时gradle下载失败的解决方法
    阿里云菜鸡上云实例
    如何用手机控制pc鼠标——Java开发与学习记实
    LeetCode刷题笔记---6月
    编译原理复习题
  • 原文地址:https://www.cnblogs.com/syyy/p/6616628.html
Copyright © 2020-2023  润新知