【Android】xml和Java兩種方式實現發送郵件頁面

在這里插入圖片描述
三三要成為安卓糕手

一: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;并依…