美文网首页
Android布局,常用控件

Android布局,常用控件

作者: 规诫 | 来源:发表于2019-07-24 12:00 被阅读0次

Android布局以及各自作用

1.LinearLayout实现效果以及属性,权重
线性布局

< LinearLayout 线性布局
    xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools" 
     命名空间,只在根布局中使用
     android:layout_width="match_parent" 布局宽的属性  
     match_parent指的是充满整个父容器   
     wrap_content 自适应,会根据自身的大小来确定自己的宽或高
     android:layout_height="match_parent"布局高的属性
     android:orientation="horizontal" 
     规定内容组件排列方式为水平 vertical(垂直)
     android:gravity=""  
     控制内部组件的对齐方式    bottom(底部)|right(右边)
     center_vertical(垂直居中)+center_horizontal(水平居中)=center(中心位置)
    android:background="@drawable/kugou" 为组件添加一个背景
    >

2.RelativeLayout实现效果以及属性,margin和padding的区别
相对布局,放置的控件默认都在左上角,所有控件都堆积到一起,我们要给每一个控件设定位置,控件才能排布好

   <RelativeLayout 子元素相对父容器来摆放:
    android:layout_alignParentLeft="true" 和父容器左对齐
    android:layout_alignParentRight="true" 和父容器右对齐
    android:layout_alignParentTop="true" 和父容器顶部对齐
    android:layout_alignParentBottom="true" 和父容器底部对齐
      android:layout_centerHorizontal="true" 水平居中
      android:layout_centerVertical="true"  垂直居中
      android:layout_centerInParent="true"  中心位置
      >

margin: 组件外边距,其他控件相对于本控件的边缘距离

<RelativeLayout 
    android:layout_margin="20dp"        距离上下左右20dp
    android:layout_marginLeft="20dp"    距离左边20dp
    android:layout_marginTop="30dp"     距离上边30dp
    android:layout_marginRight="40dp"   距离右边40dp
    android:layout_marginBottom="50dp"  距离下边50dp
        >

padding: 组件内边距, 控件中的子元素,相对于这个控件的边距距离

<RelativeLayout 
    android:padding="10dp"  距离内部内容上下左右都是10dp
    android:paddingLeft="10dp"  距离内部内容左边的10dp
    android:paddingTop="20dp"   距离内部内容上边的20dp
    android:paddingRight="30dp" 距离内部内容右边的30dp
    android:paddingBottom="40dp" 距离内部内容下边的40dp>

3.FrameLayout实现效果以及属性
帧布局:所有控件默认都在左上角堆积到一起,并且后面的控件会掩盖前面的控件

- 效果图.jpg
<FrameLayout 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"
    tools:context="${relativePackage}.${activityClass}" >
    <TextView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#FF6140" />
    <TextView
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:background="#7CFF00" />
    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#FFFF00" />
</FrameLayout>

4.TableLayout
表格布局:就是确定表格的行数,以及使用 那三个属性来设置每一行中的第某列的元素隐藏,拉伸,或者收缩即可!

  • android:collapseColumns:设置需要被隐藏的列的序号
    android:shrinkColumns:设置允许被收缩的列的列序号
    android:stretchColumns:设置运行被拉伸的列的列序号
    以上这三个属性的列号都是从0开始算的,比如shrinkColunmns = "2",对应的是第三列!
    可以设置多个,用逗号隔开比如"0,2",如果是所有列都生效,则用"*"号即可
    除了这三个常用属性,还有两个属性,分别就是跳格子以及合并单元格,这和HTML中的Table类似:
    android:layout_column="2":表示的就是跳过第二个,直接显示到第三个格子处,从1开始算的!
    android:layout_span="4":表示合并4个单元格,也就说这个组件占4个单元格

collapseColumns(隐藏列)
流程:在TableRow中定义5个按钮后,接着在最外层的TableLayout中添加以下属性: android:collapseColumns = "0,2",就是隐藏第一与第三列


运行效果图.jpg

代码如下:

<TableLayout  
    android:id="@+id/TableLayout2"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:collapseColumns="0,2" >  
    <TableRow>  
        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="one" />  

        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="two" />  

        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="three" />  

        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="four" />  

        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="five" />  
    </TableRow>  
</TableLayout>

stretchColumns(拉伸列)
流程:在TableLayout中设置了四个按钮,接着在最外层的TableLayout中添加以下属性: android:stretchColumns = "1"


运行效果.jpg

设置第二列为可拉伸列,让该列填满这一行所有的剩余空间,代码如下:

<TableLayout    
    android:id="@+id/TableLayout2"    
    android:layout_width="fill_parent"    
    android:layout_height="wrap_content"    
    android:stretchColumns="1" >    
    <TableRow>    
    
        <Button    
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:text="one" />    
    
        <Button    
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:text="two" />    
    
        <Button    
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:text="three" />    
    
        <Button    
            android:layout_width="wrap_content"    
            android:layout_height="wrap_content"    
            android:text="four" />                 
    </TableRow>    
</TableLayout>  

shrinkColumns(收缩列)
步骤:这里为了演示出效果,设置了5个按钮和一个文本框,在最外层的TableLayout中添加以下属性: android:shrinkColumns = "1"


运行截图.jpg

设置第二个列为可收缩列,代码如下:

<TableLayout  
    android:id="@+id/TableLayout2"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:shrinkColumns="1" >  

    <TableRow>  

        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="one" />  

        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="two" />  

        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="three" />  

        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="four" />  

        <Button  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="five" />  

        <TextView  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:text="文本XX" />  
    </TableRow>  
</TableLayout>
## 常用控件
1.EditText输入框组件的属性和使用(内容的长度,文本提示,密码,数字限制),以及获取文本内容
    他也是继承自TextView
    属性:

        android:password="true"              // 以”.”形式显示文本
        android:phoneNumber=”true”  //EditText输入的文字为电话号码
        android:maxLength=“50”    //EditText字数限制的设置
        android:numeric=”integer”   //只接受数字,一共有三种分别为
        integer(正整数)、signed(带符号整数,有正负)和decimal(浮点数)
        android:hint=”默认文字” //设置默认显示的文字
        android:textColorHint=”#FF0000″  //设置默认显示文字颜色
        android:enabled=”false”  //设置输入框不能被编辑,true是可以被编辑
        android:digits=”1234567890.+-*/%\n()” //限制输入框中只能输入自己
        定义的这些字符串 如果输入其它将不予以显示
2.TextView文字组件的使用和属性设置

<TextView android:id="@+id/tv_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="哈哈"
android:textColor="#FFFFFF"
android:gravity="center"
android:textSize="40sp"
android:background="#FF6140"
android:drawableTop="@drawable/ic_launcher"
android:drawableLeft="@drawable/ic_launcher"
android:drawablePadding="20dp"/>


3.res/values下可以有的所有资源,以及使用(color string dimen xml中引用)
    string 在values文件夹下的strings.xml中直接定义字符串常量
                <string name="select">选择器</string>
    
    color  在values文件夹下定义colors.xml 直接定义颜色常量
                <color name="blue">#1017C8</color>
    dimen  在values文件夹下定义dimen.xml 直接定义尺寸值 
                <dimen name="kuan">150dp</dimen>

相关文章

  • Android布局,常用控件

    Android布局以及各自作用 1.LinearLayout实现效果以及属性,权重线性布局 2.RelativeL...

  • 第二章笔记分享

    Android开发布局详解 比较常用的控件 三 WebView:加载网页 ListView:显示列表 GridVi...

  • Android - Navigation

    Android 基础知识 1. Android 常用控件 2. 控件常用属性 Android 常用知识点 动态权限...

  • Android第四讲小结

    涉及到布局(Layout)和常用控件(View) 1、线性布局和相对布局 2、比较常用的控件View(宽高、颜色、...

  • 第四次课堂小结

    涉及到布局(Layout)和常用控件(View) 1、线性布局和相对布局 2、比较常用的控件View(宽高、颜色、...

  • 第四次Android课堂笔记

    一,涉及到布局(Layout)和常用控件(View) 1线性布局和相对布局 2比较常用的控件View(宽高、颜色、...

  • 第五章小结

    一,涉及到布局(Layout)和常用控件(View) 1线性布局和相对布局 2比较常用的控件View(宽高、颜色、...

  • Andrid第一课

    相对布局是指按着控件之间的相对位置来进行布局。相对布局是Android布局中常用的一种,主要用到的标签有:Rela...

  • 面试总结

    掌握Android四大组件,常用的布局文件,自定义控件等; Android四大基本组件分别是Activity,Se...

  • SeekBar

    Android-SeekBar进度条的使用Android控件与布局——基础控件SeekBar

网友评论

      本文标题:Android布局,常用控件

      本文链接:https://www.haomeiwen.com/subject/eoztrctx.html