Android相對布局(RelativeLayout)常用屬性、練習使用按鍵、文本框等控件、線性布局(LinearLayout)屬性

RelativeLayout中子控件常用屬性:

子控件默認是從父控件的左上角開始排列的

  • 相對于父控件
 android:layout_alignParentTop="true"     和父控件的頂部對齊android:layout_alignParentBottom="true"  和父控件的底部對齊android:layout_alignParentRight="true"   和父控件的右端對齊android:layout_alignParentLeft="true"    和父控件的左端對齊
  • 相對于給定的ID控件
android:layout_above="@id/cat1"        控件的底部置于給定ID的控件之上
android:layout_below="@id/cat1"        控件底部置于給定ID的控件之下·
android:layout_toRightOf="@id/cat1"    控件的左邊緣與給定ID的控件的右邊緣對齊
android:layout_toLeftOf="@id/cat1"     控件的右邊緣與給定ID的控件的左邊緣對齊
android:layout_alignBottom="@id/cat1"  與給定控件的底邊緣對齊
android:layout_alignLeft="@id/cat1"    與給定控件的左邊緣對齊
android:layout_alignTop="@id/cat1"     與給定控件的定邊緣對齊
android:layout_alignRight="@id/cat1"   與給定控件的右邊緣對齊
android:layout_alignBaseline="@id/cat1"控件的Baseline與給定ID控件的Baseline對齊,其實這個baseline相當于筆記本里寫文字時候的底下的那條線android:layout_alignRight 本元素的右邊緣和某元素的的右邊緣對齊
android:layout_toRightOf  在某元素的右邊
  • 居中
 android:layout_centerHorizontal="true"    水平居中android:layout_centerVertical="true"      垂直居中android:layout_centerInParent="true"      相對于父控件處在正中央

一個簡單的登錄界面:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><RelativeLayoutandroid:layout_height="150dp"android:layout_width="400dp"android:layout_centerInParent="true"android:background="#ff0000"><TextViewandroid:id="@+id/user"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="用戶:"android:textSize="20dp"android:textColor="#ffffff"/><EditTextandroid:id="@+id/userline"android:layout_marginTop="5dp"android:layout_width="300dp"android:layout_height="40dp"android:layout_toRightOf="@id/user"/><TextViewandroid:id="@+id/passwd"android:layout_marginTop="10dp"android:layout_below="@id/user"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密碼:"android:textSize="20dp"android:textColor="#ffffff"/><EditTextandroid:id="@+id/passwdline"android:layout_width="300dp"android:layout_height="40dp"android:layout_below="@id/userline"android:layout_toRightOf="@id/passwd"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="確定"android:layout_alignParentRight="true"android:layout_below="@id/passwdline"/></RelativeLayout>
</RelativeLayout>

所的結果:
在這里插入圖片描述
布局間的所屬關系:
在這里插入圖片描述

Margin和Padding:

盒模型(控件)主要定義四個區域:內容 (content)、邊框距(padding)即內邊距、邊界(border)和外邊距(margin)。 對于初學者,經常會搞不清楚margin,background-color,background- image,padding,content,border之間的層次、關系和相互影響。這里提供一張盒模型的平面示意圖,希望便于你的理解和記憶。Margin 是整體移動,帶著控件里面的內容(content),而padding 是移動控件里面的內容相對于控件Bprder的距離。
在這里插入圖片描述
將上述界面進行美化:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><RelativeLayoutandroid:id="@+id/laout1"android:layout_height="150dp"android:layout_width="400dp"android:layout_centerInParent="true"android:background="#ff0000"><TextViewandroid:id="@+id/user"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:text="用戶:"android:textSize="20dp"android:textColor="#ffffff"/><EditTextandroid:id="@+id/userline"android:layout_marginTop="5dp"android:layout_width="300dp"android:layout_height="40dp"android:layout_toRightOf="@id/user"/><TextViewandroid:id="@+id/passwd"android:layout_marginTop="20dp"android:layout_below="@id/user"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:text="密碼:"android:textSize="20dp"android:textColor="#ffffff"/><EditTextandroid:id="@+id/passwdline"android:layout_width="300dp"android:layout_height="40dp"android:layout_below="@id/userline"android:layout_toRightOf="@id/passwd"/><Buttonandroid:id="@+id/btn1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_marginLeft="90dp"android:text="確定"android:layout_below="@id/passwdline"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="取消"android:layout_toRightOf="@id/btn1"android:layout_below="@id/passwdline"android:layout_marginLeft="80dp"/></RelativeLayout><RelativeLayoutandroid:id="@+id/layout2"android:background="#ff0000"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_below="@id/laout1"android:layout_marginTop="10dp"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="100dp"android:src="@drawable/picture2"/></RelativeLayout>
</RelativeLayout>

如下圖所示:
在這里插入圖片描述

安卓按鍵快速美化:

  • 在res/drawable目錄下新建按鈕樣式文件 btn_normal.xml(正常狀態) 和 btn_pressed.xml(按下狀態)。

btn_normal.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><!-- 圓角的半徑 --><corners android:radius="10dp"/><!-- 填充顏色 --><solid android:color="#3a8fea"/>
</shape>

btn_pressed.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><!-- 圓角的半徑 --><corners android:radius="10dp"/><!-- 填充顏色 --><solid android:color="#0662f5"/>
</shape>
  • 在res/drawable目錄下新建樣式文件 btn_selector.xml 文件,定義按鈕的不同狀態樣式。

btn_selector.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><!-- 正常狀態 --><item android:drawable="@drawable/btn_normal" android:state_pressed="false"/><!-- 按下狀態 --><item android:drawable="@drawable/btn_pressed" android:state_pressed="true"/></selector>

練習制作刷卡界面:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg_shopping_menu"tools:context=".MainActivity" ><ImageView android:layout_height="wrap_content"android:layout_width="wrap_content"android:layout_centerInParent="true"android:src="@drawable/pic_rf"/><ImageViewandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:layout_centerInParent="true"android:src="@drawable/card"android:paddingLeft="100dp"android:paddingTop="50dp"/><Buttonandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:layout_centerHorizontal="true"android:layout_marginBottom="30dp"android:layout_alignParentBottom="true"android:text="刷卡"android:background="@drawable/btn_selector"android:textColor="#ff0000"/><RelativeLayoutandroid:layout_height="wrap_content"android:layout_width="match_parent"android:background="#00ff00"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="30dp"android:text="刷卡界面"android:layout_marginLeft="20dp"/><Buttonandroid:id="@+id/butn1"android:layout_height="wrap_content"android:layout_width="wrap_content"android:text="注冊"android:layout_alignParentRight="true"     /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="查詢信息"android:layout_toLeftOf="@id/butn1"android:layout_marginRight="20dp"/></RelativeLayout></RelativeLayout>

結果如下:
在這里插入圖片描述
LinearLayout常用屬性(它的父控件還是RelativeLayout,所以RelativeLayout的屬性還可以拿來用):

  • orientation: 布局中控件的排列方式,有vertical(豎直,默認)horizontal(水平)兩種方式
  • gravity:控制組件所包含的子元素的對齊方式,可多個組合,如left|buttom(這個是基礎控件相對于父控件來說的)
  • layout_gravity: 控制該組件在父容器中的對齊方式,(這個是布局控件相對于父控件來說的)
  • layout_width:布局寬度,通常不直接寫數字的,用wrap_content(組件實際大小)fill_parent或者match_marent(填滿父容器)
  • layout_height:布局的高度,參數同上
  • background:為組件設置一個背景圖片,或者直接用顏色覆蓋

Weight(權重):

該屬性用來等比例地劃分區域。

  • 最簡單的用法:要等比例劃分,分誰,誰為0,Weight按比例即可
  • 當我們設置寬度為0dp時,同時設置weight屬性為1,意思就是在寬度方向上所占比重為1。如果將height設置為0,同時設置weight屬性為2,意思就是在豎直方向所占比重為2。

divider分割線:

該屬性用于LinearLayout設置分割圖片,通過showDivider來設置分割線的所在位置,有四個可選值:nonemiddlebeginingend,當然還可以通過:

  • divider: 為LinearLayout設置分割線的圖片
  • showDivider: 設置分割線所在位置,有四個通道:nonemiddlebeginingend
  • dividerPadding: 設置分割線的Padding

設置分割線(divider):
在這里插入圖片描述
在這里插入圖片描述
然后編輯該分割線的代碼:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line">      shape是分割線的形狀<sizeandroid:width="200dp"  分割線的寬android:height="2dp"   分割線的高/><strokeandroid:color="#000000"  這個是分割線的顏色/>
</shape>

使用線性布局和相對布局寫一個丑陋的登錄界面:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg_shopping_menu"tools:context=".MainActivity" ><LinearLayoutandroid:layout_width="300dp"android:layout_height="wrap_content"android:layout_centerInParent="true"android:orientation="horizontal" ><LinearLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:divider="@drawable/divider"android:showDividers="middle|end"android:dividerPadding="2dp"android:orientation="vertical" ><TextViewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="center"android:textStyle="bold"android:textSize="15dp"android:text="賬號:" /><TextViewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="center"android:textStyle="bold"android:textSize="15dp"android:text="密碼:" /><TextViewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="center"android:textStyle="bold"android:textSize="15dp"android:text="ID:" /></LinearLayout><LinearLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="5"android:orientation="vertical" ><EditTextandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" /><EditTextandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" /><EditTextandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" /></LinearLayout></LinearLayout></RelativeLayout>

結果如下圖所示:
在這里插入圖片描述

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

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

相關文章

Seata AT模式

基本思路 先決條件 支持本地ACID事務的關系數據庫。通過JDBC訪問數據庫的Java應用程序。 整體機制 從兩個階段提交協議的演變&#xff1a; 階段1&#xff1a;在同一本地事務中提交業務數據和回滾日志&#xff0c;然后釋放本地鎖和連接資源。階段2&#xff1a; 對于提交情況…

自媒體各大平臺收益對比_哪些自媒體平臺沒有新手期,適合小白擼收益?

2-26把我設置為星標&#xff0c;不錯過每一次的干貨&#xff5e;各大自媒體平臺的新手期總是讓小白們望而卻步&#xff0c;特別是百家號審核非常嚴格&#xff0c;如果沒思路、沒人指導&#xff0c;過新手有難度。不過&#xff0c;還是有很多平臺對新手寶寶很友好&#xff0c;沒…

Linux關于文件的權限筆記

1、調整文件的權限命令&#xff1a;chmodLinux的每個文件都定義了文件的擁有者&#xff1a;u(user)、擁有組&#xff1a;g&#xff08;group&#xff09;、其他人&#xff1a;o&#xff08;others&#xff09;權限&#xff0c;對應的權限用rwx的組合來定義。使用chmod命令&…

presentViewController和pushViewController

iPhone開發中從一個視圖跳到另一個視圖有三種方法&#xff1a;1、self.view addSubView:view 、self.window addSubView,需要注意的是&#xff0c;這個方法只是把頁面加在當前頁面。此時在用self.navigationControler.pushViewController和 pushViewController 是不行的。要想使…

啟動rrt什么意思_python學習第144課--創建虛擬機、設置虛擬機參數以及啟動虛擬機...

【每天幾分鐘&#xff0c;從零入門python編程的世界&#xff01;】上節我們介紹了下載虛擬機以及centOS的相關事項&#xff0c;現在我們創建虛擬機。●創建虛擬機安裝好virtualbox之后&#xff0c;我們點擊Oracle VM VirtualBox打開管理器&#xff0c;界面如下圖&#xff1a;你…

Android按鍵響應的幾種方式、安卓頁面的跳轉、頁面跳轉傳參、頁面自動跳轉、Activity(頁面)的生命周期

按鍵響應的第一種方式&#xff1a; 在XML文件里面設置按鍵的onClick綁定函數。就像下面的代碼&#xff0c;給Button設置onClick屬性&#xff0c;在按鍵被按下的時候&#xff0c;會調用java文件里面的onClickbtton1這個函數&#xff0c;因為現已將它和這個按鈕進行了綁定。利用…

pycharm體驗

查看python版本 退出 exit()

Linux文本檢索命令grep筆記

grep是在linux系統中基于行文本非常實用檢索工具&#xff0c;通過該命令可以將匹配到的結果信息輸出到終端控制臺。語法格式&#xff1a;grep [-ivnc] 需要匹配的內容 文件名常用參數說明&#xff1a;-i 檢索的時候不區分大小寫-c 檢索到的匹配行數-n 檢索到的匹配行顯式具體的…

-生成樹的相關問題

瓶頸生成樹 無向圖G的一顆瓶頸生成樹(bottleneck spanning tree)。T是這樣的一顆生成樹&#xff0c;它最大的邊權值在G的所有生成樹中是最小的。瓶頸生成樹的值為T中最大權值邊的權。 即生成樹中最長邊最短的樹。 無向圖的最小生成樹一定是瓶頸生成樹&#xff0c;但瓶頸生成樹不…

成立出版社的條件_創始人親述:法國鴻飛文化出版社的誕生故事和做書心得

鴻飛文化出版社2007年在法國創立&#xff0c;專門出版由中國作家和法國插畫師共同創作的圖畫書。創始人葉俊良引用蘇東坡的詩句“人生到處知何似&#xff0c;應似飛鴻踏雪泥。泥上偶然留指爪&#xff0c;鴻飛那復計東西。”為出版社取名&#xff0c;希望通過這些“印跡”為讀者…

python單行注釋和多行注釋

# 后面加一個空格&#xff0c;下面就不會有波浪線了 點擊小燈泡下的第一行&#xff0c;可以調整注釋格式 多行注釋

Linux文件查詢筆記

1、數據庫查找命令&#xff1a;locateLinux也可以通過locate命令查找文件&#xff0c;locate命令主要是依據一個數據庫文件來執行文件的查找&#xff0c;默認情況下Linux每天會默認檢索系統中的所有文件&#xff0c;然后把檢索的文件信息記錄到數據庫文件中。因為運行locate命令…

柱坐標系下的ns方程_麥克斯韋方程組小結

一、▽ 算子、點積、叉積l▽ 算子叫“del”算子&#xff0c;即<< span"">?/?x,?/?y,?/?z>,可以理解為一個符號向量&#xff0c;向量里的元素是偏微分運算符號&#xff0c;沒有任何具體意義&#xff0c;只是一個表示方法。ln維向量的內積定義如下&a…

Java高級補充(Socket服務端、客戶端)、Handler類、安卓Socket傳數據、WebView

Socket服務端簡單地API&#xff1a; 主要用到以下幾個API&#xff1a;ServerSocket用來設置端口號、accept用來和服務端連接、getInputStream用來獲取輸入流、read用來讀取輸入流里面的數據&#xff0c;存放在提前開辟好的緩沖區里面。 import java.io.IOException; import j…

向anna學習系統結構和測試流程

今天上午主要是anne簡單介紹系統的功能和公司的測試流程。通過她的講解對于系統的大概流程有了概念。但是具體的問題估計還得到實際的操作中去。可能是這一塊的而功能比較老了&#xff0c;所以相關的文檔也是沒有及時更新的。看起來比較蠻煩&#xff0c;等我全部弄清楚了有時間…

Linux進程終止命令kill或killall?筆記

在linux命令下&#xff0c;如果需要終止某個進程&#xff0c;可以使用kill或者killall等命令來實現。終止命令的原理都是向linux內核發送一個系統操作的信號以及某個進程的ID&#xff0c;然后系統內核會根據指定的進程ID進行相應的處理。kill命令典型的用法&#xff1a;首先使用…

貝葉斯分析好壞_貝葉斯統計 | 第五章第一部分 決策基本概念

逃不掉線上期中考。。。。。。。。。開攝像頭屏錄拍照上傳。。。。。。20號考貝葉斯&#xff0c;18號考多元&#xff0c;時間序列考試時間還未知。。。。。。。但筆記還是要記的。不過貝葉斯已經跟上進度了&#xff0c;之后會按課程進度來更新貝葉斯的內容。今天開始會更新時間…

Visual Studio的調試技巧

Visual Studio的調試技巧 【原文地址】 Debugging Tips with Visual Studio 2010 【原文發表日期】 2010/8/19 10:48 AM 這是我寫的關于VS2010和.Net4發布的博客系列的第26篇。 今天的博文包含了一些有用的能用于VS的調試技巧。 我的朋友 Scott Cate&#xff08;他寫了很多很…

讀者寫者問題

河北科技大學操作系統課程設計——讀者寫者問題 #include <windows.h> #include <conio.h> #include <stdlib.h> #include <fstream> #include <io.h> #include <string.h> #include <stdio.h> using namespace std; #define READE…