前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Study Jams_ViewGroups&&LinearLayout

Study Jams_ViewGroups&&LinearLayout

作者头像
MaybeHC
发布2024-04-23 18:11:06
580
发布2024-04-23 18:11:06
举报
文章被收录于专栏:技术之路技术之路

ViewGroup

一.什么是ViewGroup

ViewGroup相当于是一个放置View的容器,里面可以放置其他的View,如TextView,ImageView等等

大家可以先观察下面这段代码

这里写图片描述
这里写图片描述

这段代码 LinearLayout 布局就相当于一个ViewGroups ,里面的两个TextView就相当于是LinearLayout的子View ,LinearLayout就是父View(放置View的容器)

LinearLayout布局

LinearLayout又称为线性布局,这个布局会将它所包含的控件在线性方向上依次排列 下面是LinearLayout布局的代码

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Google"
        android:textSize="22sp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="study Jams"
        android:textSize="22sp"/>
</LinearLayout>
1.android:orientation

相信大家通过看这段代码,大部分的内容都可以看懂,只有在这一行的时候有所疑问android:orientation=”vertical”我们通过查文档得知

这里写图片描述
这里写图片描述

android:orientation=”“属性指定了排列的方向,vertical垂直排列,horizontal水平排列 这是上面代码呈现出来的视图

这里写图片描述
这里写图片描述

如果将android:orientation=”vertical”改为horizontal视图会是什么样?

这里写图片描述
这里写图片描述
2.wrap_content &match_parent

下面为大家介绍设置宽度高度的两种属性 wrap_content 和match_parent,当我们直接设置View宽度时,因为不知道内容多少,很容易造成内容的损失,如下图的第一种,所以我们一般使用其余的两种属性、wrap_context 适合内容大小的宽度,View包含内容|match_parent 与父视图的宽度相同,如下图所示

这里写图片描述
这里写图片描述
3.android :layout_weight布局权重

接下来我们学习LinearLayout中的一个重要属性android :layout_weight.这个属性允许我们使用比例的方式来指定控件的大小 权重的默认大小为0,当我们不设置时,该View的权重就为0. 如何使用权重? 首先我们需要把使用权重的高度/宽度设置为0dp,使用权重控制大小。 下面的代码和呈现是我们不使用权重时的

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#f00"
        android:text="Google"
        android:textSize="22sp"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="study Jams"
        android:background="#0f0"
        android:textSize="22sp"/>
</LinearLayout>
这里写图片描述
这里写图片描述

下来我们使用权重,将第一个TextView的layout_width设置为0dp,layout_weight=1,得到如下图示

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#f00"
        android:text="Google"
        android:textSize="22sp"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="study Jams"
        android:background="#0f0"
        android:textSize="22sp"/>
</LinearLayout>
这里写图片描述
这里写图片描述

LinearLayout就为大家介绍到这里了 很感谢Google Study Jams的活动,推动我的学习

本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2024-04-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ViewGroup
    • 一.什么是ViewGroup
    • LinearLayout布局
      • 1.android:orientation
        • 2.wrap_content &match_parent
          • 3.android :layout_weight布局权重
          相关产品与服务
          容器服务
          腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
          http://www.vxiaotou.com