【Android】xml和Java两种方式实现发送邮件页面


三三要成为安卓糕手

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

1:xml代码

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">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入联系人" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入主题" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="top"
        android:hint="请输入内容" />

    <Button
        android: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的区别 ```xml android:gravity="right" //文字位置靠右 android:layout_gravity="right" //布局靠右 ``` ![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://i-blog.csdnimg.cn/direct/ade4bffb9b2e49c78fb239b9aa69b55d.png) ## 二:Java操控LinearLayout布局参数 ### 1:getLayoutParams() ```java EditText etCont = findViewById(R.id.et_cont); LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) etCont.getLayoutParams();//强制向下转型 layoutParams.weight = 1f; ``` 获取LinearLayout下子控件EditText的布局参数;继承关系如下 ![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://i-blog.csdnimg.cn/direct/4c9ee199572e46bcb6b8f0a43af03a93.png) ![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://i-blog.csdnimg.cn/direct/fe14a632598b4626b43a883c398697c9.png) ![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://i-blog.csdnimg.cn/direct/0e44922178084708b27b7549d1a82067.png) 所以可以直接设置权重weight ### 2:效果 ![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://i-blog.csdnimg.cn/direct/c61df5aa33284935826f9fa9fe671813.png) ### 3:添加按钮 ```java LinearLayout root = findViewById(R.id.main); Button button = new Button(this); button.setText("test"); root.addView(button); ``` ![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://i-blog.csdnimg.cn/direct/52392dff5839469dafd79b3d031780fb.png) ### 4:给按钮设置参数(进阶) #### (1)设置布局和权重 体悟:重在new布局对象这行代码上, ```java 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); ``` ![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://i-blog.csdnimg.cn/direct/8bb6979100444610a7d57ecba66e8021.png) 真无语啊 ![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://i-blog.csdnimg.cn/direct/6c38859d84354cc4b7ed85f8f36b694f.png) ## 三:Java实现发送邮件界面 虽然xml控制布局非常的好用,但是作为一名java开发者,用java代码去控制布局也是需要去掌握的老弟!! 被ex到了:总结一下步骤 第一步:创建布局对象 ```java LinearLayout linearLayout = new LinearLayout(this); ``` 第二步:为 LinearLayout 设置布局参数(两种方式) 匿名内部类 ```java linearLayout.setLayoutParams( new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); ``` 创建参数 ```java ViewGroup.LayoutParams params = new ViewGroup.LayoutParams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT); linearLayout.setLayoutParams(params); ``` ### 1:设置父布局参数 ```java 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 的主布局显示在屏幕上。 ![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://i-blog.csdnimg.cn/direct/f479c105093149babd501be52b6747ca.png) 等价写法 ```java 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:为控价设置布局参数 ```java //为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`** **的特有属性** (如 `weight`、`gravity`),则**必须使用** **`LinearLayout.LayoutParams`** : ### 4:为控件还是控件中的内容设置参数 问题引入 ![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://i-blog.csdnimg.cn/direct/8313a10d01de4041b4626c9499808974.png) 设置了top为什么还在中间 ![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://i-blog.csdnimg.cn/direct/ac23a3d4a46e43c8abc74e6a09981371.png) 上述图片中比较了xml和Java中,究竟是给控件本身设置参数,还是给控件中的内容设置参数 总结一下:简单说,前者管 "View 自己在父布局哪",后者管 "View 内部内容摆在哪",应用场景和作用对象有明显区分 。 ### 5:代码总结 写完难度就不大了,好桑心\~\~\~ ```java 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 { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /** * 匿名内部类设置LinearLayout的布局参数 */ // linearLayout.setLayoutParams( // new ViewGroup.LayoutParams( // ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); //设置父布局LinearLayout LinearLayout 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); //联系人的EditText EditText editContact = new EditText(this); editContact.setHint("请输入联系人"); LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); editContact.setLayoutParams(params1); //主题的EditText EditText editSubject = new EditText(this); editSubject.setHint("请输入主题"); LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); editSubject.setLayoutParams(params2); //内容的EditText EditText 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); } } ``` ![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传](https://i-blog.csdnimg.cn/direct/24856535f5e64353b55a8421b331c311.png)

相关推荐
hqxstudying2 小时前
J2EE模式---前端控制器模式
java·前端·设计模式·java-ee·状态模式·代码规范·前端控制器模式
ZeroToOneDev5 小时前
Java(LinkedList和ArrayList底层分析)
java·开发语言
没有bug.的程序员6 小时前
JAVA面试宝典 -《 架构演进:从单体到 Service Mesh》
java·面试·架构
2501_915918416 小时前
iOS WebView 调试实战 localStorage 与 sessionStorage 同步问题全流程排查
android·ios·小程序·https·uni-app·iphone·webview
典学长编程6 小时前
Java从入门到精通!第十一天(Java常见的数据结构)
java·开发语言·数据结构
皮皮林5516 小时前
设计一个多租户 SaaS 系统,如何实现租户数据隔离与资源配额控制?
java·saas
霍格沃兹软件测试开发6 小时前
Playwright 自动化测试系列(6)| 第三阶段:测试框架集成指南:参数化测试 + 多浏览器并行执行
java·数据库·mysql·自动化
Bonnie_12157 小时前
02-netty基础-java四种IO模型
java·开发语言·nio·jetty
Digitally7 小时前
如何永久删除安卓设备中的照片(已验证)
android·gitee
我不是星海7 小时前
建造者设计模式
java·开发语言