Android studio之UI组件

布局管理器

布局可以嵌套,布局里面可以写一堆布局和View

1.线性布局(LinearLayout)

默认靠左靠上

最常用属性:

​ android:id(标识) android:layout_margin(外边距)

​ android:layout_width(宽度) android:layout_padding(内边距)

​ android:layout_height(高度) android:orientation(方向)

​ android:background(背景)

用法

android:layout_width="match_parent" //匹配父控件的宽度
android:layout_width="wrap_content" //包含内容的宽度,就是内容有多少就有多宽
android:orientation="vertical" //垂直方向布局
android:orientation="horizontal"//
android:background="#000000" //后面跟颜色的代码
android:id="@+id/accessibility_custom_action_11"//资源名字
android:layout_width="200dp"//自己写宽度或高度——高度宽度单位一般用dp,字体用sp
View是所有控件的父类,首字母要大写,不然会变灰色
android:padding="20dp"//表上下左右都空20dp
android:paddingLeft="20dp"//左边
android:paddingBottom="20dp"//下边
android:paddingRight="20dp">//右边
android:paddingTop="20dp"//上
android:layout_marginTop="20dp">//与上一个控件的距离
android:layout_marginBottom="20dp"//与下一个控件的距离
android:layout_marginLeft="15dp"//左边距
android:layout_marginRight="15dp"//右边距
android:gravity="bottom"//内部元素在左下角
android:gravity="center" //内部元素在中间
android:gravity="right"//内部元素在
android:gravity="center_vertical"//垂直居中
android:gravity="center_horizontal"//水平居中
android:layout_weight="1"//全中,里面写数字,多个时,按分数平分,如果子控件有占dp的话要先减去,然后把剩下的平分(一般设为0dp)
    
    <?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"
    **droid:orientation="vertical"**
    tools:context=".MainActivity">
    <!--开始是默认水平排列的,android:orientation="vertical"这个是后面才加的-->
    <LinearLayout
        android:id="@+id/accessibility_custom_action_11"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#000000"
        android:orientation="vertical"
        android:padding="20dp"
        android:layout_marginBottom="20dp">

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FF0033" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="#0066FF"
        android:orientation="horizontal"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
       >
        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#000000"
            android:layout_weight="1"/>
<!--            默认是从左从上开始排列,所以在左上角-->
        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#FF0033"
            android:layout_weight="1"/>
        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#55AA99"
            android:layout_weight="1"/>
    </LinearLayout>

</LinearLayout>

2.相对布局(RelativeLayout)

默认都是靠左靠上

  1. 最常用属性

    android:layout_toLeftOf(在谁的左边) android:layout_below(在谁的下边)

    android:layout_toRightOf(在谁的右边)
    android:layout_alignBottom(跟谁的底部对齐)

    android:layout_alignParentBottom(跟父控件底部对齐)

用法

android:layout_alignParentBottom="true"//靠父控件底部对齐,true就是执行
android:layout_alignParentRight="true"//父控件右边
android:layout_toRightOf="@id/view_1"//表在@id/view_1的右边
android:layout_below="@id/view_1"//表在@id/view_1的下边

套娃娃

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <View
       android:id="@+id/view_1"
       android:layout_width="100dp"
       android:layout_height="100dp"
       android:background="#000000"
       />
   <View
       android:id="@+id/view_2"
       android:layout_width="100dp"
       android:layout_height="100dp"
       android:background="#FF0033"
       android:layout_below="@id/view_1"/>
   
<LinearLayout
    android:id="@+id/view_3"
    android:layout_width="match_parent"
    android:layout_height="200"
    android:layout_below="@id/view_2"
    android:background="#0066FF"
    android:orientation="horizontal"
    android:padding="15dp">
   <View
       android:layout_width="100"
       android:layout_height="match_parent"
       android:background="#FF0033"/>
   <RelativeLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="#000000"
       android:padding="15dp">
      <View
          android:id="@+id/view_4"
          android:layout_width="100dp"
          android:layout_height="match_parent"
          android:background="#FF9900" />
      <View
          android:id="@+id/view_5"
          android:layout_width="100dp"
          android:layout_height="match_parent"
          android:background="#ffffff" 
          android:layout_toRightOf="@id/view_4"
          android:layout_marginLeft="10dp"/>
   </RelativeLayout>
   
</LinearLayout>
    
</RelativeLayout>
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值