2、Android基础控件(2)(常用布局和滚动条)

关于Android线性布局与滚动实现 关于android 布局中的滚动条实现 最近关于android的布局需要实现一个功能就是能够实现水平和垂直滚动,关于这个问题解决方案如下: 在线性布局中进行嵌套,以列为主;在设计好的布局的外层进行滚动条设置; 首先将进行行列的设计,尽量多的让btn比手机的像素值大这样才能够进行滚动。 相关代码如下: <HorizontalScrollView android:la 阅读详情

3、常用布局

线性布局内部LinearLayout (orientation)

线性布局内部的各视图有两种排列方式:

(1)orientation属性值为horizontal时,内部视图在水平方向从左往右排列。

(2)orientation属性值为vertical时,内部视图在垂直方向从上往下排列。 如果不指定orientation属性,则LinearLayout默认水平方向排列。

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="横排第一个"
            android:textSize="17sp"
            android:textColor="#000000"
            android:textAlignment="center"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="横排第二个"
            android:textSize="17sp"
            android:textColor="#000000"
            android:textAlignment="center"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="竖排第一个"
            android:textSize="17sp"
            android:textColor="#000000"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="竖排第二个"
            android:textSize="17sp"
            android:textColor="#000000"
           />
    </LinearLayout>
</LinearLayout>

 相对布局RelativeLayout(layout_centerInParent)

相对布局的下级视图位置由其他视图决定。用于确定下级视图位置的参照物分两种:

(1)与该视图自身平级的视图;

(2)该视图的上级视图(也就是它归属的RelativeLayout) 如果不设定下级视图的参照物,那么下级视图默认显示在RelativeLayout内部的左上角。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="150dp" >

    <TextView
        android:id="@+id/tv_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#eeeeee"
        android:text="我在中间"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/tv_center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="#eeeeee"
        android:text="我在水平中间"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/tv_center_vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:background="#eeeeee"
        android:text="我在垂直中间"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/tv_parent_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:background="#eeeeee"
        android:text="我跟上级左边对齐"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/tv_parent_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="#eeeeee"
        android:text="我跟上级右边对齐"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/tv_parent_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="#eeeeee"
        android:text="我跟上级顶部对齐"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/tv_parent_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#eeeeee"
        android:text="我跟上级底部对齐"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/tv_left_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/tv_center"
        android:layout_alignTop="@+id/tv_center"
        android:background="#eeeeee"
        android:text="我在中间左边"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/tv_right_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/tv_center"
        android:layout_alignBottom="@+id/tv_center"
        android:background="#eeeeee"
        android:text="我在中间右边"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/tv_above_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tv_center"
        android:layout_alignLeft="@+id/tv_center"
        android:background="#eeeeee"
        android:text="我在中间上面"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/tv_below_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_center"
        android:layout_alignRight="@+id/tv_center"
        android:background="#eeeeee"
        android:text="我在中间下面"
        android:textColor="#000000" />
</RelativeLayout>

网格布局GridLayout 

网格布局默认从左往右、从上到下排列,它新增了两个属性:

(1) columnCount属性,它指定了网格的列数,即每行能放多少个视图;

(2) rowCount属性,它指定了网格的行数,即每列能放多少个视图;

<!-- 根布局为两行两列的网格布局,其中列数由columnCount指定,行数由rowCount指定 -->
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2"
    android:rowCount="2">

    <TextView
        android:layout_width="180dp"
        android:layout_height="60dp"
        android:gravity="center"
        android:background="#ffcccc"
        android:text="浅红色"
        android:textColor="#000000"
        android:textSize="17sp" />

    <TextView
        android:layout_width="180dp"
        android:layout_height="60dp"
        android:gravity="center"
        android:background="#ffaa00"
        android:text="橙色"
        android:textColor="#000000"
        android:textSize="17sp" />

    <TextView
        android:layout_width="180dp"
        android:layout_height="60dp"
        android:gravity="center"
        android:background="#00ff00"
        android:text="绿色"
        android:textColor="#000000"
        android:textSize="17sp" />

    <TextView
        android:layout_width="180dp"
        android:layout_height="60dp"
        android:gravity="center"
        android:background="#660066"
        android:text="深紫色"
        android:textColor="#000000"
        android:textSize="17sp" />

</GridLayout>

 滚动视图

(1) ScrollView,它是垂直方向的滚动视图;垂直方向滚动时,layout_width属性值设置为match_parent,layout_height属性值设置为wrap_content。

(2) HorizontalScrollView,它是水平方向的滚动视图;水平方向滚动时,layout_width属性值设置为wrap_content,layout_height属性值设置为match_parent。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- HorizontalScrollView是水平方向的滚动视图,当前高度为200dp -->
    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="200dp">

        <!-- 水平方向的线性布局,两个子视图的颜色分别为青色和黄色 -->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <View
                android:layout_width="300dp"
                android:layout_height="match_parent"
                android:background="#aaffff" />

            <View
                android:layout_width="300dp"
                android:layout_height="match_parent"
                android:background="#ffff00" />
        </LinearLayout>
    </HorizontalScrollView>

    <!-- ScrollView是垂直方向的滚动视图,当前高度为自适应 -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!-- 垂直方向的线性布局,两个子视图的颜色分别为绿色和橙色 -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <View
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:background="#00ff00" />

            <View
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:background="#ffffaa" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

 

解决OPPO手机安卓5.1版本flex布局横向滚动条失效 解决OPPO手机安卓5.1版本flex布局横向滚动条失效 解决OPPO手机安卓5.1版本flex布局横向滚动条失效,首先尝试flex新旧兼容写法无果后。观察到项目中宽度全部失效,累计宽度最大为容器的宽度,考虑是否是由于容器空间不足,所有项目被迫缩小。将容器中所有项目的flex-shrink属性设置为0后,问题解决。 正常效果 bug效果 有关flex-shrink属性的基本用法 flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。 计算方法: 当.. 阅读详情

相关推荐

Android ScrollView 滚动条 4 种样式实战:insideOverlay/outsideInset 布局差异解析

本文深入解析Android ScrollView滚动条的4种样式(insideOverlay/outsideInset等),详细对比其布局差异与适用场景。通过代码示例和实测数据,展示不同滚动条样式对padding、边距和内容布局的影响,并提供动态切换、性能优化等进阶技巧,帮助开发者精准控制滑动滚动条的行为与视觉效果。

cigang4063的博客 353

【Android】相对布局(RelativeLayout)最全解析

相对布局(RelativeLayout)是一种根据父容器和兄弟控件作为参照来确定控件位置的布局方式。使用相对布局,需要将布局节点改成

lucashu的博客 6万+

Android——使用ScrollView实现滚动效果,当内容超出屏幕范围时自动滑动显示

需要注意的是,在ScrollView中只能包含一个直接子视图(例如LinearLayout),因此如果需要在ScrollView中放置多个视图,需要使用一个容器布局将它们包裹起来。首先,我们需要在XML布局文件中声明一个ScrollView,并将需要滚动的内容放置在该ScrollView的内部。在Android中,使用ScrollView非常简单,我们只需将需要滚动的内容放置在ScrollView的内部即可。接下来,我们可以通过动态添加子视图的方式来添加需要滚动的内容,也可以直接在XML布局文件中添加。

CyberFlare的博客 1747

RelativeLayout相对布局详解

RelativeLayout相对布局在Android UI开发中也应用比较多,虽然它不及LinearLayout使用方便,但某些场景中使用RelativeLayout也是一种很不错的选择。在官网上介绍是一个视图组,在相对位置显示子视图。是用于设计用户界面的非常强大的实用程序,因为它可以消除嵌套视图组并保持布局层次结构平坦,从而提高性能。如果您发现自己使用了多个嵌套组,则可以将它们替换为单个.

chuyouyinghe的专栏 3140

Android中实现滚动效果的方法

滚动是Android应用程序开发中常见的交互效果之一,它可以在用户界面中展示大量内容,并提供流畅的浏览体验。在本篇文章中,我将介绍几种在Android应用中实现滚动效果的方法,并提供相应的源代码示例。综上所述,这是在Android应用中实现滚动效果的几种常见方法。接下来,将需要滚动的内容放置在ScrollView标签内部,可以使用LinearLayout或其他布局容器包裹需要滚动的视图。RecyclerView是一个更灵活和高效的滚动容器,它支持垂直和水平滚动,并提供了更多的自定义选项和优化措施。

ByteSparkX的博客 1055

安卓学习笔记09:常用布局 - 帧式布局

文章目录零、学习目标一、帧式布局1、帧式布局特点2、继承关系图3、常用属性二、案例演示 —— 切换颜色1、创建安卓应用【SwitchColor】2、主布局资源文件activity_main.xml3、字符串资源文件strings.xml4、主界面MainActivity5、启动应用,查看效果6、修改切换颜色的算法三、课后作业任务:自动切换颜色 零、学习目标 能说出帧式布局常用的属性 能利用帧式布局实现简单的界面设计 一、帧式布局 1、帧式布局特点 帧式布局是一种层叠式的布局,后添加的控件会层叠在先添加的

howard2005的专栏 1643

Android Studio应用基础,手把手教你从入门到精通(小白学习)总结2 之 常用界面布局和ListView

Android studio小白使用手册系列二来啦,系列一链接见文内详情

CV在读 5209

安卓布局文件相关属性

android:id —— 为控件指定相应的ID android:layout_width —— 定义本元素的宽度 android:layout_height —— 定义本元素的高度 android:background —— 指定该控件所使用的背景...

暴走影视 483

android 布局滚动条,Android 布局控件——滚动条视图,日期,时间

今天学长讲了一些控件,比较强的那种控件。刚开始讲了图片,但是图片我前面写过了就跳过。滚动条牛牛们应该很熟悉,也常用哈。 这是垂直的滚动条视图哈一起来用吧!android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/scrollView"android:fillViewport="tr...

weixin_30074929的博客 743

RelativeLayout(相对布局)用法实例讲解

本节引言 LinearLayout也是我们用的比较多的一个布局,我们更多的时候更钟情于他的weight(权重)属性,等比例划分,对屏幕适配还是帮助蛮大的;但是使用LinearLayout的时候也有一个问题,就是当界面比较复杂的时候,需要嵌套多层的 LinearLayout,这样就会降低UI Render的效率(渲染速度),而且如果是listview或者GridView上的 item,效率会更低,另外太多层LinearLayout嵌套会占用更多的系统资源,还有可能引发stackoverflow; 但是如果我们使用RelativeLayout的话,可能仅仅需要一层就可以完成了,以父容器或者兄弟组件

ubuntu18.04 显卡驱动问题 造成屏幕分辨变大 NVIDIA-SMI has failed because it couldn't communicate with ....等问题

问题1:装好了显卡驱动,cudnn,tensorflow等没问题,一直可以用。但是突然有一天不行了,终端输入:nvidia-smi 显示:NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed...

梦萦飞--CV/DL/ML 2248

android安卓开发常用布局属性归纳整理

【代码】android安卓开发常用布局属性归纳整理。

技术应让更广泛的人群流入到实际产品开发中才是正统 1278

OpenRadar Matlab版:毫米波雷达信号处理全流程开源库详解

毫米波雷达数据解析与信号处理是感知领域的基础能力。本文介绍毫米波雷达从ADC原始采样到距离-多普勒(Range-Doppler)热图、CFAR检测、DOA估计直至点云与生命体征参数提取的完整处理链路,并深入讲解OpenRadar(Matlab版)开源库的内核设计、核心API与实测经验。从原理上看,距离FFT与多普勒FFT构成RD谱,CFAR自适应阈值实现目标检测,相位解缠与滤波用于呼吸心跳提取。技术价值在于将繁琐的雷达数据读取、矩阵变换和算法流程标准化,免去重复造轮子。可应用于人员存在感知、生命体征监护、4

weixin_33806859的博客 322

android 横向点击滚动条,小程序scroll-view安卓机隐藏横向滚动条的实现

一、实践踩坑项目使用mpvue开发1.使用flex布局// html大概长这样 // css相应就大概长这样.worth-wraper{margin-top: 60rpx;height: 560rpx;box-sizing: border-box;display: flex;width: 750rpx;overflow: hidden;font-size: 28rpx...

weixin_32938207的博客 292

立创EDA开源足轮机器人:基于STM32F407与ESP32的LQR自平衡与FOC驱动设计全解析

本文详细解析了基于立创EDA平台的开源足轮机器人项目。该项目采用STM32F407作为主控,ESP32负责无线通信,并应用LQR自平衡算法与FOC驱动技术,实现了机器人的稳定站立、移动转向及复杂地形适应能力。文章深入剖析了其硬件架构、软件设计及调试经验,为机器人运动控制学习提供了宝贵参考。

weixin_42608299的博客 232

【第六届起重机创意大赛国一作品——资料分享】

文章目录前言一、比赛内容分析二、确定方案与作品模型1.方案简图2.作品模型三、电控部分控制思路单片机选择与资源分配拓展板的设计接线与走线代码分享总结

qq_19308801的博客 7783

Android原生基础控件

Android常用UI控件及属性解析 本文总结了Android开发中常用的UI控件及其核心属性。主要内容包括: 通用属性:如layout_width/height、id、margin/padding、gravity等基本设置 TextView:字体大小、颜色、单行显示、跑马灯效果等文本处理 EditText:输入类型、提示文字、最大长度等输入控制 ImageView:图片显示与背景设置 Button:4种点击事件实现方式(内部类、匿名内部类、接口实现、XML绑定) ProgressBar:圆形和水平进度条样

m0_73444363的博客 1127

别再让地图加载慢吞吞!GeoServer + GeoWebCache 缓存配置实战(附性能对比)

本文详细介绍了如何通过GeoServer和GeoWebCache优化地图服务性能,实现地图加载速度的显著提升。从缓存机制解析到实战配置流程,再到高级性能调优技巧,帮助开发者构建高效地图缓存系统,解决地图加载慢的问题。

weixin_42525369的博客 407

AndroidStudio滚动条

滚动条有两种增加方式,从xml文件和从java文件,我主要记录了从xml文件增加。 有水平和竖直方向两种滚动条,同时,默认一个滚动条下面只能有一个控件,如果想要 添加多个控件,就需要在这些控件的外层增加一个布局,下面shi线性布局。 <!--水平方向的滚动条--> <HorizontalScrollView android:layout_width="w...

qq_43586051的博客 6237

SAP-ABAP:SAP HANA性能神器FDA:是极速查询的引擎,也是CPU过载的元凶

在SAP S/4HANA的世界里,有一个被ABAP开发者们既爱又怕的特性——Fast Data Access(FDA),它能将性能提升百倍,也能让CPU瞬间飙升。本文将带您全面解剖这一核心机制。

baidu_35680696的博客 849

从 on_event 到 lifespan:FastAPI 生命周期管理的旧方法与新方法对比与迁移

从 on_event 到 lifespan:FastAPI 生命周期管理的旧方法与新方法对比与迁移

XiaoqiangClub的博客 1021

android滚动条布局横向,小程序scroll-view安卓机隐藏横向滚动条的实现

一、实践踩坑项目使用mpvue开发1.使用flex布局// html大概长这样 // css相应就大概长这样.worth-wraper{margin-top: 60rpx;height: 560rpx;box-sizing: border-box;display: flex;width: 750rpx;overflow: hidden;font-size: 28rpx...

weixin_30994671的博客 260

27、自闭症儿童治疗与玩具设计研究

本研究探讨了自闭症儿童的治疗方法与玩具设计之间的关联,分析了游戏疗法、音乐疗法和职业疗法等在治疗中的应用,并介绍了多种针对自闭症儿童的治疗玩具案例。通过用户研究和多学科合作,提出了以Pro-Ability为核心的设计理念,开发了结合音乐疗法和精细运动训练的智能玩具套件。研究总结了当前成果与局限性,并展望了未来发展方向,旨在为自闭症儿童提供更有效的康复支持。

keras9composer的博客 211

【Android】安卓开发常用的布局

前言布局是安卓开发中很基础的部分,作为移动开发的初学者,我也在不断的学习进步中~正文文件的位置一般都在layout文件夹内。一个布局文件,一般对应一个Activity,在代码中通过setContentView(R.layout.first_layout);将页面跟逻辑关联起来布局的分类常见的布局LinearLayout,线性布局方式 一种使用单个水平行或垂直行来组织子项的布局。它会在窗口长度超出

Code Metaverse 4664

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

android 左右滚动条,小程序scroll-view安卓机隐藏横向滚动条的实现详解

一、实践踩坑项目使用mpvue开发1.使用flex布局// html大概长这样 // css相应就大概长这样.worth-wraper{margin-top: 60rpx;height: 560rpx;box-sizing: border-box;display: flex;width: 750rpx;overflow: hidden;font-size: 28rpx;color: #7a7...

weixin_29840475的博客 491

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

android RecyclerView 添加可拖动的滚动条

实现的效果图如下,为了效果这里我设置的宽了一些,实际根据需求修改即可 先说几个属性: android:scrollbars" 一般都是垂直的滚动条设置vertical android:scrollbarSize 设置滚动条的宽度 android:scrollbarThumbVertical:设置垂直滚动条颜色 android:scrollbarTrackVertical :设置滚动条背景色 个人习惯吧这几个属性都放在了style 里面,没有直接放在recylerview 的布局.

小牧在一直在学习,在前进的道路上大家一起学习,进步。 7646

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

上一篇: 2、Android 基础控件(1)(文本显示和视图基础)
下一篇: Android Studio如何格式化XML代码顺序
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小郭同学忒骚了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值