美文网首页
布局管理器

布局管理器

作者: Amy木婉清 | 来源:发表于2020-09-22 19:18 被阅读0次

Android的布局主要有以下几个:
线性布局(LInearLayout)、相对布局(RelativeLayout)、帧布局(FrameLayout)、表格布局(TableLayout)、绝对布局(absoluteLayout)、网格布局(GridLayout)、约束布局(ConstraintLayout)
相对布局中需要注意的两个属性:

android:gravity="center" //控件显示的位置
android:ignoreGravity="@id/tv_1" //某一控件不受影响

相对布局管理器内部类
RelativeLayout.LayoutParams
相对布局的相关属性及其解释:

相对于父元素给控件布局
android:layout_centerHrizontal  水平居中 
android:layout_centerVertical 垂直居中 
android:layout_centerInparent    相对于父元素完全居中 
android:layout_alignParentBottom 位于父元素的下边缘 
android:layout_alignParentLeft   位于父元素的左边缘 
android:layout_alignParentRight  位于父元素的右边缘 
android:layout_alignParentTop    位于父元素的上边缘 
android:layout_alignWithParentIfMissing  如果对应的兄弟元素找不到的话就以父元素做参照物 
属性值必须为id的引用名“@id/id-name”
    android:layout_below      位于元素的下方 
    android:layout_above      位于元素的的上方 
    android:layout_toLeftOf   位于元素的左边 
    android:layout_toRightOf  位于元素的右边 
    android:layout_alignTop   该元素的上边缘和某元素的的上边缘对齐 
    android:layout_alignLeft  该元素的左边缘和某元素的的左边缘对齐 
    android:layout_alignBottom 该元素的下边缘和某元素的的下边缘对齐 
    android:layout_alignRight  该元素的右边缘和某元素的的右边缘对齐 
给属性赋予像素值
    android:layout_marginBottom      底边缘的距离 
    android:layout_marginLeft           左边缘的距离 
    android:layout_marginRight         右边缘的距离 
    android:layout_marginTop           上边缘的距离

EditText的android:hint :设置EditText为空时输入框内的提示信息

线性布局(LinearLayout)

线性布局首先要规定排列方向,分别是垂直排列和水平排列:

  android:orientation="horizontal"//水平排列
  android:orientation="vertical" //垂直排列
动态修改指定文字颜色:
String text = "获得银宝箱!";  
SpannableStringBuilder style=new SpannableStringBuilder(text);     
style.setSpan(new BackgroundColorSpan(Color.RED),2,5,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);     //设置指定位置textview的背景颜色  
style.setSpan(new ForegroundColorSpan(Color.RED),0,2,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);     //设置指定位置文字的颜色  
textView.setText(style);  

android:layout_weight="1"//权重 控件剩余部分等分

登录页面的适用:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".LoginActivity">
   <EditText
       android:id="@+id/et_1"
       android:paddingBottom="20dp"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="QQ号/微信号/Email"
       />
  <EditText
      android:id="@+id/et_2"
      android:paddingBottom="20dp"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:hint="请输入密码"
      />
  <Button
      android:layout_marginTop="20dp"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="登录"
      android:textColor="@android:color/white"
      android:background="#ff009688"
    />
  <TextView
      android:layout_marginTop="20dp"
      android:gravity="center"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="登录遇到问题?"
      android:layout_gravity="center"
    />
</LinearLayout>

帧布局:


framelayout.png
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:foreground="@mipmap/ic_launcher"
    android:foregroundGravity="right|bottom"
    tools:context=".FrameActivity">
    <TextView
        android:layout_width="280dp"
        android:layout_height="280dp"
        android:layout_gravity="center"
        android:background="#2196F3"
        android:text="方框1"
        android:textColor="#ffffff"
        />
    <TextView
        android:layout_width="230dp"
        android:layout_height="230dp"
        android:layout_gravity="center"
        android:background="#009688"
        android:text="方框2"
        android:textColor="#ffffff"
        />
    <TextView
        android:layout_width="180dp"
        android:layout_height="180dp"
        android:layout_gravity="center"
        android:background="#9C27B0"
        android:text="方框3"
        android:textColor="#ffffff"
        />

</FrameLayout>
表格布局
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <!--表格布局下角标从0开始-->
    <!-- android:collapseColumns="1"隐藏第二列-->
    <!--android:stretchColumns="1"设置第二列允许被拉伸-->
    <!--android:shrinkColumns="1"允许第二列被收缩-->
    <TableRow>
        <Button
            android:id="@+id/btn_table"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮1"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮2"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮33333333333333333333333333"
            />
    </TableRow>
    <TableRow>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮4"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮5"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮666666666666666666666666666"
            />
    </TableRow>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮666666666666666666666666666"
        />
<TableRow>
    <TextView
        android:gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="账号"
        android:textSize="18sp"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入账号"
        />
</TableRow>

</TableLayout>

相关文章

  • 二、Android布局管理器

    1、布局管理器的继承关系 Android布局管理器本身也是UI组件,所有的布局管理器都是ViewGroup的子类,...

  • 迟到的java总结

    1.默认布局管理器 1.1 Java的Jframe和Jwindow的默认布局管理器为边界式布局管理器 1.2 而J...

  • RecyclerView 的三种LayoutManager

    RecyclerView 推出了三种布局管理器:1、LinearLayoutManager 线性布局管理器,呈现线...

  • PYQT5布局管理

    Qt布局管理按简单分可分为绝对位置布局和布局管理器布局 一、绝对位置布局: 组件不放在布局管理器中,通过函数set...

  • tkinter 布局管理

    1 布局管理器 布局管理器是负责管理各组件的大小和位置的。此外,当用户调整窗口大小后,布局管理器还会自动调整窗口中...

  • Java基础08GUI

    Java 基础07IO GUI四大布局 FlowLayout(流式布局管理器)它是Panel默认的布局管理器。 B...

  • 【Android 性能优化】Android布局优化解读

    Android的布局管理器本身就是个UI组件,所有的布局管理器都是ViewGroup的子类,而ViewGroup是...

  • Java进阶学习笔记(十一)

    1、控制反转: 1.1 布局管理器: frame成为容器中的部件,部件管理容器---布局管理器; 1.2 控制反转...

  • 用AWT编写用户界面二之布局管理器

    布局管理器 :为了实现跨平台的特效并且获得动态的布局结果,java 将容器内的所有组件安排给一个“布局管理器”负责...

  • SWT&JFace 常用布局

    常用布局管理器 Java的布局:FlowLayout、BorderLayout、GridLayout、GridBa...

网友评论

      本文标题:布局管理器

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