Android學習之路(3) 布局

線性布局LinearLayout

前幾個小節的例程中,XML文件用到了LinearLayout布局,它的學名為線性布局。顧名思義,線性布局 像是用一根線把它的內部視圖串起來,故而內部視圖之間的排列順序是固定的,要么從左到右排列,要 么從上到下排列。在XML文件中,LinearLayout通過屬性android:orientation區分兩種方向,其中從左 到右排列叫作水平方向,屬性值為horizontal;從上到下排列叫作垂直方向,屬性值為vertical。如果LinearLayout標簽不指定具體方向,則系統默認該布局為水平方向排列,也就是默認android:orientation="horizontal".

下面做個實驗,讓XML文件的根節點掛著兩個線性布局,第一個線性布局采取horizontal水平方向,第 二個線性布局采取vertical垂直方向。然后每個線性布局內部各有兩個文本視圖,通過觀察這些文本視圖 的排列情況,從而檢驗線性布局的顯示效果。詳細的XML文件內容如下所示:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="橫排第一個"android:textSize="17sp"android:textColor="#000000" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="橫排第二個"android:textSize="17sp"android:textColor="#000000" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="豎排第一個"android:textSize="17sp"android:textColor="#000000" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="豎排第二個"android:textSize="17sp"android:textColor="#000000" /></LinearLayout>
</LinearLayout>

運行測試App,進入如下圖所示的演示頁面,可見horizontal為橫向排列,vertical為縱向排列,說明android:orientation的方向屬性確實奏效了。

除了方向之外,線性布局還有一個權重概念,所謂權重,指的是線性布局的下級視圖各自擁有多大比例 的寬高。比如一塊蛋糕分給兩個人吃,可能兩人平均分,也可能甲分三分之一,乙分三分之二。兩人平 均分的話,先把蛋糕切兩半,然后甲分到一半,乙分到另一半,此時甲乙的權重比為1:1。甲分三分之 一、乙分三分之二的話,先把蛋糕平均切成三塊,然后甲分到一塊,乙分到兩塊,此時甲乙的權重比為1:2。就線性布局而言,它自身的尺寸相當于一整塊蛋糕,它的下級視圖們一起來分這個尺寸蛋糕,有的 視圖分得多,有的視圖分得少。分多分少全憑每個視圖分到了多大的權重,這個權重在XML文件中通過 屬性android:layout_weight來表達。

把線性布局看作蛋糕的話,分蛋糕的甲乙兩人就相當于線性布局的下級視圖。假設線性布局平均分為左 右兩塊,則甲視圖和乙視圖的權重比為1:1,意味著兩個下級視圖的layout_weight屬性都是1。不過視圖 有寬高兩個方向,系統怎知layout_weight表示哪個方向的權重呢?所以這里有個規定,一旦設置了layout_weight屬性值,便要求layout_width填0dp或者layout_height填0dp。如果layout_width填0dp,則layout_weight表示水平方向的權重,下級視圖會從左往右分割線性布局;如果layout_height填0dp,則layout_weight表示垂直方向的權重,下級視圖會從上往下分割線性布局。 按照左右均分的話,線性布局設置水平方向horizontal,且甲乙兩視圖的layout_width都填0dp,layout_weight都填1,此時橫排的XML片段示例如下:

<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="橫排第一個"android:textSize="17sp"android:textColor="#000000" /><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="橫排第二個"android:textSize="17sp"android:textColor="#000000" />
</LinearLayout>

按照上下均分的話,線性布局設置垂直方向vertical,且甲乙兩視圖的layout_height都填0dp,layout_weight都填1,此時豎排的XML片段示例如下:

<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:layout_width="wrap_content"android:layout_height="0dp"android:layout_weight="1"android:text="豎排第一個"android:textSize="17sp"android:textColor="#000000" /><TextViewandroid:layout_width="wrap_content"android:layout_height="0dp"android:layout_weight="1"android:text="豎排第二個"android:textSize="17sp"android:textColor="#000000" />
</LinearLayout>

把上面兩個片段放到新頁面的XML文件,其中第一個是橫排區域采用紅色背景(色值為ff0000),第二 個是豎排區域采用青色背景(色值為00ffff)。重新運行測試App,打開演示界面如下圖所示,可見橫 排區域平均分為左右兩塊,豎排區域平均分為上下兩塊。

相對布局RelativeLayout

線性布局的下級視圖是順序排列著的,另一種相對布局的下級視圖位置則由其他視圖決定。相對布局名 為RelativeLayout,因為下級視圖的位置是相對位置,所以得有具體的參照物才能確定最終位置。如果不設定下級視圖的參照物,那么下級視圖默認顯示在RelativeLayout內部的左上角。 用于確定下級視圖位置的參照物分兩種,一種是與該視圖自身平級的視圖;另一種是該視圖的上級視圖 (也就是它歸屬的RelativeLayout)。綜合兩種參照物,相對位置在XML文件中的屬性名稱說明見下表。

為了更好地理解上述相對屬性的含義,接下來使用RelativeLayout及其下級視圖進行布局來看看實際效果圖。下面是演示相對布局的XML文件例子:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="150dp" ><TextViewandroid:id="@+id/tv_center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:background="#ffffff"android:text="我在中間"android:textSize="11sp"android:textColor="#000000" /><TextViewandroid: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:textSize="11sp"android:textColor="#000000" /><TextViewandroid: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:textSize="11sp"android:textColor="#000000" /><TextViewandroid: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:textSize="11sp"android:textColor="#000000" /><TextViewandroid: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:textSize="11sp"android:textColor="#000000" /><TextViewandroid: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:textSize="11sp"android:textColor="#000000" /><TextViewandroid: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:textSize="11sp"android:textColor="#000000" /><TextViewandroid: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:textSize="11sp"android:textColor="#000000" /><TextViewandroid: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:textSize="11sp"android:textColor="#000000" /><TextViewandroid: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:textSize="11sp"android:textColor="#000000" /><TextViewandroid: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:textSize="11sp"android:textColor="#000000" />
</RelativeLayout>

上述XML文件的布局效果如下圖所示,RelativeLayout的下級視圖都是文本視圖,控件上的文字說明 了所處的相對位置,具體的控件顯示方位正如XML屬性中描述的那樣。

Java程序如下:

package com.example.chapter03;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;public class RelativeLayoutActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_relative_layout);}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/39308.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/39308.shtml
英文地址,請注明出處:http://en.pswp.cn/news/39308.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Android之版本號、版本別名、API等級對應關系(全)(一百六十二)

簡介&#xff1a; CSDN博客專家&#xff0c;專注Android/Linux系統&#xff0c;分享多mic語音方案、音視頻、編解碼等技術&#xff0c;與大家一起成長&#xff01; 優質專欄&#xff1a;Audio工程師進階系列【原創干貨持續更新中……】&#x1f680; 人生格言&#xff1a; 人生…

HTML詳解連載(4)

HTML詳解連載&#xff08;4&#xff09; 專欄鏈接 [link](http://t.csdn.cn/xF0H3)下面進行專欄介紹 開始嘍CSS定義書寫位置示例注意 CSS引入方式內部樣式表&#xff1a;學習使用 外部演示表&#xff1a;開發使用代碼示例行內樣式代碼示例 選擇器作用基礎選擇器標簽選擇器舉例特…

RISC-V公測平臺發布 · 7-zip 測試

簡介 7-Zip 是一個開源的壓縮和解壓縮工具&#xff0c;具有高壓縮比和快速解壓縮的特點。除了普通的文件壓縮和解壓縮功能之外&#xff0c;7-Zip 還提供了基準測試功能&#xff0c;通過壓縮和解壓縮大型文件來評估系統的處理能力和性能。 7-Zip 提供了一種在不同壓縮級別和多…

BUUCTF [MRCTF2020]Ezpop解題思路

題目代碼 Welcome to index.php <?php //flag is in flag.php //WTF IS THIS? //Learn From https://ctf.ieki.xyz/library/php.html#%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E9%AD%94%E6%9C%AF%E6%96%B9%E6%B3%95 //And Crack It! class Modifier {protected $var;publi…

運維監控學習筆記7

Zabbix的安裝&#xff1a; 1、基礎環境準備&#xff1a; 安裝zabbix的yum源&#xff0c;阿里的yum源提供了zabbix3.0。 rpm -ivh http://mirrors.aliyun.com/zabbix/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm 這個文件就是生成了一個zabbix.repo 2、安…

流程挖掘in汽車丨寶馬的流程效能提升實例

汽車行業在未來10年里&#xff0c;可能會面臨比過去50年更多的變化。電動化、智能化、共享化和自動駕駛等方面的趨勢可能給企業流程帶來以下挑戰&#xff1a; 供應鏈管理-電動化和智能化的發展可能導致供應鏈中的零部件和系統結構發生變化&#xff0c;企業需要重新評估和優化供…

zookeeperAPI操作與寫數據原理

要執行API操作需要在idea中創建maven項目 &#xff08;改成自己的阿里倉庫&#xff09;導入特定依賴 添加日志文件 上邊操作做成后就可以進行一些API的實現了 目錄 導入maven依賴&#xff1a; 創建日志文件&#xff1a; 創建API客戶端&#xff1a; &#xff08;1&#xff09…

Springboot 實踐(5)springboot添加資源訪問目錄及目錄測試

前文講解了swagger測試服務控制器&#xff0c;實現了數據庫數據訪問&#xff0c;這些功能都是運行在后臺服務器上&#xff0c;實際用戶并不能直接調用接口獲取數據&#xff0c;即使用戶能夠利用接口獲取到數據&#xff0c;數據也是結構化數據&#xff0c;不能爭取轉化成用戶使用…

基于OFDM+64QAM系統的載波同步matlab仿真,輸出誤碼率,星座圖,鑒相器,鎖相環頻率響應以及NCO等

目錄 1.算法運行效果圖預覽 2.算法運行軟件版本 3.部分核心程序 4.算法理論概述 2.1 OFDM原理 2.2 64QAM調制 2.3 載波同步 5.算法完整程序工程 1.算法運行效果圖預覽 2.算法運行軟件版本 MATLAB2022a 3.部分核心程序 ............................................…

【從零學習python 】31.深入理解Python中的高階函數和閉包

文章目錄 高階函數定義一個變量指向函數高階函數函數做為另一個函數的參數函數作為另一個函數的返回值 閉包函數嵌套什么是閉包修改外部變量的值原因分析解決方案 進階案例 高階函數 在Python中&#xff0c;函數其實也是一種數據類型。 def test():return hello worldprint(t…

NestJs 中使用 mongoose

在 NestJS 中鏈接 MongoDB 有兩種方法。一種方法就是使用TypeORM來進行連接&#xff0c;另外一種方法就是使用Mongoose。 此筆記主要是記錄使用Mongoose的。所以我們先安裝所需的依賴&#xff1a; npm i nestjs/mongoose mongoose安裝完成后&#xff0c;需要在AppModule中引入…

SpringBoot后端服務開啟Https協議提供訪問(使用阿里云資源)

目錄 概述 申請/下載證書 部署證書 本地測試訪問 服務器部署訪問 最后/擴展 總結 概述 本篇博客說明如何將SpringBoot項目開啟Https協議提供訪問。 博文以步驟【申請/下載證書】&#xff0c;【部署證書】&#xff0c;【本地測試訪問】&#xff0c;【服務器部署訪問】 &a…

SIP/VoIP之常見的視頻問題

除了語音通話外&#xff0c;視頻通話也是SIP協議通話中重要的功能&#xff0c;在實際應用中&#xff0c;經常會遇到一些視頻問題&#xff0c;如下&#xff08;以h264為例&#xff09; 一、 己方未顯示對方視頻圖像 排查方法&#xff1a; 查看網絡抓包中有沒有發給已方的視頻…

LORA開發板采集溫濕度數據,連接PC上位機顯示和液晶屏顯示

一、準備材料 準備以下板子和器件 Lora開發板x2 USB數據線x2 OLED 屏幕x2 StLink下載器x1 母對母杜邦線x3 DHT11 x2 二、設備連接 如圖所示先將OLED 屏幕插入到開發板中 接著按照圖中所示的&#xff0c;將串口一以及lora的撥碼開關撥到指定方向 接著將USB數據線一端插入到…

SQL Server用sql語句添加列,添加列注釋

SQL Server用sql語句添加列&#xff0c;添加列注釋 微軟文檔&#xff1a; https://learn.microsoft.com/zh-cn/sql/relational-databases/tables/add-columns-to-a-table-database-engine?viewsql-server-ver15 alter table article add RedirectURL varchar(600) nu…

(七)Unity VR項目升級至Vision Pro需要做的工作

Vision Pro 概述 定位為混合現實眼鏡&#xff0c;對AR支持更友好 無手柄&#xff0c;支持手&#xff08;手勢&#xff09;、眼&#xff08;注視&#xff09;、語音交互 支持空間音頻&#xff0c;相比立體聲、環繞聲更有沉浸感和空間感 支持VR/AR應用&#xff0c;支持多種應用模…

八字精批API接口

接口平臺&#xff1a;https://api.yuanfenju.com/ 開發文檔&#xff1a;https://doc.yuanfenju.com/ 支持格式&#xff1a;JSON 請求方式&#xff1a;HTTP POST <?php//密鑰 $api_secret "wD******XhOUW******pvr"; //請求網關 $gateway_host_url "ht…

FPGA應用學習筆記-----復位電路(二)和小結

不可復位觸發器若和可復位觸發器混合寫的話&#xff0c;不可復位觸發器是由可復位觸發器饋電的。 不應該出現的復位&#xff0c;因為延時導致了冒險&#xff0c;異步復位存在靜態冒險 附加素隱含項&#xff0c;利用數電方法&#xff0c;消除靜態冒險 這樣多時鐘區域還是算異步的…

【Docker】如何在設計 dockerfile 過程中,設置容器啟動后的定時任務

如何在設計 dockerfile 過程中&#xff0c;設置容器啟動后的定時任務 jwensh 2023.08.14 文章目錄 如何在設計 dockerfile 過程中&#xff0c;設置容器啟動后的定時任務1. 基于 alpine 設計 dockerfile 過程中&#xff0c;設置容器啟動后的定時任務2. 基于 CentOS 設計 Dockerf…

深度學習實戰基礎案例——卷積神經網絡(CNN)基于SqueezeNet的眼疾識別|第1例

文章目錄 前言一、數據準備1.1 數據集介紹1.2 數據集文件結構 二、項目實戰2.1 數據標簽劃分2.2 數據預處理2.3 構建模型2.4 開始訓練2.5 結果可視化 三、數據集個體預測 前言 SqueezeNet是一種輕量且高效的CNN模型&#xff0c;它參數比AlexNet少50倍&#xff0c;但模型性能&a…