在这里插入图片描述
三三要成为安卓糕手

一:xml中LinearLayout布局参数的使用

1:xml代码

<?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:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="10dp"tools:context=".layout.LinearLayoutActivity"><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入联系人" /><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入主题" /><EditTextandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="top"android:hint="请输入内容" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="right"android:text="发送" /></LinearLayout>

效果展示

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

2:hint-提示文本

hint 是指当文本输入框(像 EditText)为空时显示的提示文本。一旦用户开始输入内容,这个提示文本就会自动消失。

[hɪnt] ——提示

3:gravity和layout_gravity的区别

android:gravity="right"    //文字位置靠右
android:layout_gravity="right"  //布局靠右

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

二:Java操控LinearLayout布局参数

1:getLayoutParams()

		EditText etCont = findViewById(R.id.et_cont);LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) etCont.getLayoutParams();//强制向下转型layoutParams.weight = 1f;

获取LinearLayout下子控件EditText的布局参数;继承关系如下

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

所以可以直接设置权重weight

2:效果

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

3:添加按钮

        LinearLayout root = findViewById(R.id.main);Button button = new Button(this);button.setText("test");root.addView(button);

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

4:给按钮设置参数(进阶)

(1)设置布局和权重

体悟:重在new布局对象这行代码上,

        LinearLayout root = findViewById(R.id.main);Button button = new Button(this);button.setText("test");LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);//宽高layoutParams.weight = 0.3f;button.setLayoutParams(layoutParams);root.addView(button);

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

真无语啊

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

三:Java实现发送邮件界面

虽然xml控制布局非常的好用,但是作为一名java开发者,用java代码去控制布局也是需要去掌握的老弟!!

被ex到了:总结一下步骤

第一步:创建布局对象

        LinearLayout linearLayout = new LinearLayout(this);

第二步:为 LinearLayout 设置布局参数(两种方式)

匿名内部类

        linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

创建参数

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);linearLayout.setLayoutParams(params);

1:设置父布局参数

        LinearLayout linearLayout = new LinearLayout(this);ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);linearLayout.setLayoutParams(params);linearLayout.setOrientation(linearLayout.VERTICAL);linearLayout.setPadding(10,10,10,10);linearLayout.setBackgroundColor(Color.BLUE);setContentView(linearLayout); 

2:setContentView()

setContentView(linearLayout) 是 Android 开发中用于设置 Activity 界面的核心方法。它的作用是将指定的视图(如 LinearLayout)作为当前 Activity 的主布局显示在屏幕上。

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

等价写法

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
  • LinearLayout.LayoutParams 继承自 ViewGroup.LayoutParams,前者包含后者的所有功能。

3:为控价设置布局参数

//为LinearLayout设置布局ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);linearLayout.setLayoutParams(params);//为editText设置布局LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);editText.setLayoutParams(params1);

上面这两段代码其实本质上都是相通的,创建布局参数,控件在使用参数,妙~!

有一点需要注意:如果需要使用 LinearLayout 的特有属性(如 weightgravity),则必须使用 LinearLayout.LayoutParams

4:为控件还是控件中的内容设置参数

问题引入

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
设置了top为什么还在中间

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

上述图片中比较了xml和Java中,究竟是给控件本身设置参数,还是给控件中的内容设置参数

总结一下:简单说,前者管 “View 自己在父布局哪”,后者管 “View 内部内容摆在哪”,应用场景和作用对象有明显区分 。

5:代码总结

写完难度就不大了,好桑心~~~

package com.xlong.myapplication.layout;import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;import com.xlong.myapplication.R;public class EmailActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);/*** 匿名内部类设置LinearLayout的布局参数*/
//        linearLayout.setLayoutParams(
//                new ViewGroup.LayoutParams(
//                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));//设置父布局LinearLayoutLinearLayout linearLayout = new LinearLayout(this);ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);linearLayout.setLayoutParams(params);linearLayout.setOrientation(linearLayout.VERTICAL);linearLayout.setPadding(10,10,10,10);linearLayout.setBackgroundColor(Color.BLUE);setContentView(linearLayout);//联系人的EditTextEditText editContact = new EditText(this);editContact.setHint("请输入联系人");LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);editContact.setLayoutParams(params1);//主题的EditTextEditText editSubject = new EditText(this);editSubject.setHint("请输入主题");LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);editSubject.setLayoutParams(params2);//内容的EditTextEditText editContent = new EditText(this);editContent.setHint("请输入内容");LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);params3.weight = 1;editContent.setGravity(Gravity.TOP);editContent.setLayoutParams(params3);linearLayout.addView(editContact);linearLayout.addView(editSubject);linearLayout.addView(editContent);//设置Button的布局,看这里就是给button控件布局靠右Button button = new Button(this);button.setText("发送");LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);params4.gravity = Gravity.RIGHT;button.setLayoutParams(params4);linearLayout.addView(button);}
}

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/bicheng/90438.shtml
繁体地址,请注明出处:http://hk.pswp.cn/bicheng/90438.shtml
英文地址,请注明出处:http://en.pswp.cn/bicheng/90438.shtml

如若内容造成侵权/违法违规/事实不符,请联系英文站点网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

美林数据用大模型重构电能质量评估,让隐蔽合规问题无所遁形

在“双碳”目标驱动下&#xff0c;电网企业正加速推进数字化转型&#xff0c;电能质量评估作为电力系统安全运行的核心环节&#xff0c;其合规性与效率直接影响着电网智能化水平。然而&#xff0c;传统人工审核模式已难以应对海量报告与复杂标准——单份报告需20-30人天核对、关…

前端基础 JS Vue3 Ajax

一、JSalert( .... ) //弹出框console.log( ....... ) //输出到控制台浏览器JS引入方式&#xff1a;1、内部脚本&#xff1a;将JS代码定义在HTML页面中位于<script></script>标签之间2、外部脚本&#xff1a;将JS代码写在外部JS文件中&#xff0c;在HTML页面中使用…

如何解决pip安装报错ModuleNotFoundError: No module named ‘notebook’问题

【Python系列Bug修复PyCharm控制台pip install报错】如何解决pip安装报错ModuleNotFoundError: No module named ‘notebook’问题 一、摘要 在使用 PyCharm 进行 Python 开发时&#xff0c;常常需要通过 pip install 安装第三方包。但有时即便已经安装成功&#xff0c;运行代…

一、Vue概述以及快速入门

什么是VueVue的快速入门代码&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Vue快速入门</title><script src"js/vue.js"></script> </head> <bod…

模型的存储、加载和部署

定义损失函数并以此训练和评估模型 存储模型可以只存储state_dict或模型参数&#xff0c;每当需要部署经过训练的模型时&#xff0c;创建模型的对象并从文件中加载参数&#xff0c;这是 Pytorch 创建者推荐的方法。 目录 模型的存储、加载 模型的部署 模型的存储、加载 承接…

Java学习第七十部分——微服务架构

目录 一、前言提要 二、核心优势 三、核心技术栈 四、构建步骤 五、困难挑战 六、总结归纳 一、前言提要 Java 微服务架构是一种使用 Java 技术栈构建分布式系统的方法论&#xff0c;它将单一的大型应用程序分解为一组小型、独立、松耦合、可独立部署和扩展的服务。每个服…

六边形滚动机器人cad【7张】三维图+设计书明说

摘 要 机械制造业是国家的重要产业,随着时代的发展,智能化越来越在生活中变得普遍,工业的发展深深的影响着一个国家的经济发展。全球经济的发展带领着机械工业在不断的进步。随着国外先进技术在我国的传播,也影响着我国技术的发展,在全球经济的大环境的推动下,大型四边形…

人形机器人加快先进AI机器人开发

物理AI的新时代通用人形机器人专为快速适应现有的以人类为中心的城市和工业工作空间而构建&#xff0c;用以承担枯燥、重复性或对体力要求高的工作任务。这些机器人正在从工厂车间走向医疗健康机构&#xff0c;通过自动化帮助人类工作&#xff0c;缓解劳动力短缺问题。但是&…

AI 驱动开发效能跃升:企业级智能开发全流程优化方案​

企业软件开发正面临 “三高困境”&#xff1a;需求变更频率高、人力成本占比高、线上故障风险高。破解这些难题的核心在于构建 AI 驱动的全流程智能开发体系&#xff0c;通过系统化效能优化实现开发能力升级。​ 需求分析作为开发起点&#xff0c;常因理解偏差导致后期返工。A…

时序数据库 TDengine × Ontop:三步构建你的时序知识图谱

在做设备预测性维护或能源管理分析时&#xff0c;你是否也曾思考过&#xff1a;如何才能让机器“理解”我们收集的大量时序数据&#xff1f;工业现场的数据是结构化的&#xff0c;而语义分析、知识推理却往往需要 RDF 等图谱格式。换句话说&#xff0c;“会说话”的数据更聪明&…

Android启动图不拉伸且宽占满屏幕

Android启动图不拉伸且宽占满屏幕 一般启动图的做法&#xff1a; start_app_bg.xml <?xml version"1.0" encoding"utf-8"?> <layer-list xmlns:android"http://schemas.android.com/apk/res/android"><item><shape>&l…

rust-方法语法

方法语法 方法类似于函数&#xff1a;我们用 fn 关键字和一个名称来声明它们&#xff0c;它们可以有参数和返回值&#xff0c;并且包含一些在从其他地方调用该方法时运行的代码。与函数不同&#xff0c;方法是在结构体&#xff08;或枚举、trait 对象&#xff0c;分别在第6章和…

【C++】C++ 的入门语法知识1

本文主要讲解C语言的入门知识&#xff0c;包括命名空间、C的输入与输出、缺省参数以及函数重载。 目录 1 C的第一个程序 2 命名空间 1&#xff09; 命名空间存在的意义 2&#xff09; 命名空间的定义 3&#xff09; 命名空间的使用 3 C的输出与输入 1&#xff09; C中…

SpringBoot6-10(黑马)

JWT令牌简介&#xff1a;1.JWT全称:JSON Web Token(https://iwt.io/)定义了一种简洁的、自包含的格式&#xff0c;用于通信双方以json数据格式安全的传输信息。2.组成: >第一部分:Header(头)&#xff0c;记录令牌类型、签名算法等。例如:("alg":“HS256",“t…

智能制造场景195个术语的16个分类

说明&#xff1a;《智能制造典型场景参考指引&#xff08;2025年版&#xff09;》日前&#xff0c;由工信部办公厅正式发布&#xff0c;将成为众多制造型企业的工作纲领 1. 工厂数字化规划设计&#xff08;1.1&#xff09;&#xff1a;在电脑上用专业软件设计工厂布局、规划生产…

[论文阅读] 人工智能 + 软件工程 | 微信闭源代码库中的RAG代码补全:揭秘工业级场景下的检索增强生成技术

微信闭源代码库中的RAG代码补全&#xff1a;揭秘工业级场景下的检索增强生成技术 论文标题&#xff1a;A Deep Dive into Retrieval-Augmented Generation for Code Completion: Experience on WeChatarXiv:2507.18515 A Deep Dive into Retrieval-Augmented Generation for Co…

RabbitMQ—仲裁队列

上篇文章&#xff1a; RabbitMQ集群搭建https://blog.csdn.net/sniper_fandc/article/details/149312481?fromshareblogdetail&sharetypeblogdetail&sharerId149312481&sharereferPC&sharesourcesniper_fandc&sharefromfrom_link 目录 1 Raft一致性算法…

[2025CVPR-目标检测方向] CorrBEV:多视图3D物体检测

1. ​研究背景与动机​ 论文关注自动驾驶中相机仅有的多视图3D物体检测&#xff08;camera-only multi-view 3D object detection&#xff09;问题。尽管基于鸟瞰图&#xff08;BEV&#xff09;的建模近年来取得显著进展&#xff08;如BEVFormer和SparseBEV等基准模型&#xf…

oracle 数据库批量变更数据 将a表字段批量更新为b表字段

需求&#xff1a;将excel表中的数据批量更新到 taccoinfo表中vc_broker字段0、备份&#xff1a;create table taccoinfo0724 as select vc_custno ,vc_broker from taccoinfo 1、创建临时表&#xff1a; create table taccoinfo0724_1 as select vc_custno ,vc_broker from…

vim-xcode 项目常见问题解决方案

vim-xcode 项目常见问题解决方案 项目基础介绍 vim-xcode 是一个开源项目&#xff0c;旨在通过 Vim 编辑器与 Xcode 项目进行交互。该项目允许开发者在 Vim 中直接构建、测试和运行 Xcode 项目&#xff0c;从而提高开发效率。vim-xcode 主要使用 Vimscript 编写&#xff0c;并依…