android 相對布局 絕對 - CSDN

2021-02-07 CSDN技術社區
這兩個有什麼差別就不說了,很簡單。
絕對布局,例子:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <TextView android:text="紅色" android:gravity="center_horizontal|center_vertical" android:background="#aa0000" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> <TextView android:text="綠色" android:gravity="center_horizontal" android:background="#00aa00" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> <TextView android:text="藍色" android:gravity="center|bottom" android:background="#0000aa" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> <TextView android:text="黃色" android:gravity="bottom" android:background="#aaaa00" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <TextView android:text="第一行" android:textSize="15sp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:text="第二行" android:textSize="15sp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:text="第三行" android:textSize="15sp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:text="第四列" android:textSize="15sp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> </LinearLayout>




相對布局,例:

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#0000FF" android:padding="10px" > <TextView android:id="@+id/tv01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="30dp" android:text="Type here:" /> <EditText android:id="@+id/txt01" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/tv01" /> <Button android:id="@+id/btn01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="@id/txt01" android:layout_alignParentRight="true" android:text="OK" /> <Button android:id="@+id/btn02" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="@id/txt01" android:layout_toLeftOf="@id/btn01" android:layout_marginRight="30dp" android:text="Cancel" /></RelativeLayout>




絕對,相對混用,例子:


<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <TextView android:id="@+id/id_textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="id:" > </TextView> <EditText android:id="@+id/id_editText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:singleLine="true" > </EditText> <TextView android:id="@+id/name_textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="name:" > </TextView> <EditText android:id="@+id/name_editText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:singleLine="true" > </EditText> <TextView android:id="@+id/sno_textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="sno:" > </TextView> <EditText android:id="@+id/sno_editText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:singleLine="true" > </EditText> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/createButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dip" android:text="創建" > </Button> <Button android:id="@+id/insertButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/createButton" android:text="插入" > </Button> <Button android:id="@+id/queryButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/createButton" android:text="查詢全部" > </Button> <Button android:id="@+id/queryButtonSomeOne" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/queryButton" android:text="單查詢" > </Button> <Button android:layout_width="wrap_content" android:text="更新" android:layout_height="wrap_content" android:id="@+id/updateButton" android:layout_toRightOf="@+id/queryButtonSomeOne" > </Button> <Button android:layout_width="wrap_content" android:text="刪除" android:layout_height="wrap_content" android:id="@+id/deleteButton" android:layout_toRightOf="@+id/updateButton" > </Button> </RelativeLayout> <TextView android:id="@+id/displayResult" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="display result..." android:layout_gravity="center_horizontal" > </TextView>





一些常用參數:

RelativeLayout用到的一些重要的屬性: 

 

   第一類:屬性值為true或false 
   android:layout_centerHrizontal 水平居中 
    android:layout_centerVertical  垂直居中 
   android:layout_centerInparent   相對於父元素完全居中 
   android:layout_alignParentBottom 貼緊父元素的下邊緣 
   android:layout_alignParentLeft  貼緊父元素的左邊緣 
   android:layout_alignParentRight 貼緊父元素的右邊緣 
   android:layout_alignParentTop   貼緊父元素的上邊緣 
   android:layout_alignWithParentIfMissing 如果對應的兄弟元素找不到的話就以父元素做參照物 

   第二類:屬性值必須為id的引用名「@id/id-name」 
   android:layout_below     在某元素的下方 
   android:layout_above     在某元素的的上方 
   android:layout_toLeftOf  在某元素的左邊 
   android:layout_toRightOf 在某元素的右邊 

   android:layout_alignTop  本元素的上邊緣和某元素的的上邊緣對齊 
   android:layout_alignLeft 本元素的左邊緣和某元素的的左邊緣對齊 
   android:layout_alignBottom本元素的下邊緣和某元素的的下邊緣對齊 
   android:layout_alignRight 本元素的右邊緣和某元素的的右邊緣對齊 

   第三類:屬性值為具體的像素值,如30dip,40px 
   android:layout_marginBottom             離某元素底邊緣的距離 
   android:layout_marginLeft                  離某元素左邊緣的距離 
   android:layout_marginRight                離某元素右邊緣的距離 
   android:layout_marginTop                  離某元素上邊緣的距離 


EditText的android:hint 

設置EditText為空時輸入框內的提示信息。 

android:gravity  
android:gravity屬性是對該view 內容的限定.比如一個button上面的text.  你可以設置該text在view的靠左,靠右等位置.以button為例,android:gravity="right"則button上面的文字靠右 

android:layout_gravity 
android:layout_gravity是用來 設置該view相對與起父view 的位置.比如一個button在linearlayout裡,你想把該button放在靠左、靠右等位置就可以通過該屬性設置.以button為例,android:layout_gravity="right"則button靠右 

android:layout_alignParentRight
 
使當前控制項的右端和父控制項的右端對齊。這裡屬性值只能為true或false,默認false。 

android:scaleType: 
android:scaleType是控制圖片如何resized/moved來匹對ImageView的size。ImageView.ScaleType/ android:scaleType值的意義區別: 

CENTER /center 按圖片的原來size居中顯示,當圖片長/寬超過View的長/寬,則截取圖片的居中部分顯示 

CENTER_CROP / centerCrop 按比例擴大圖片的size居中顯示,使得圖片長(寬)等於或大於View的長(寬) 

CENTER_INSIDE / centerInside 將圖片的內容完整居中顯示,通過按比例縮小或原來的size使得圖片長/寬等於或小於View的長/寬 

FIT_CENTER / fitCenter 把圖片按比例擴大/縮小到View的寬度,居中顯示 

FIT_END / fitEnd  把圖片按比例擴大/縮小到View的寬度,顯示在View的下部分位置 

FIT_START / fitStart 把圖片按比例擴大/縮小到View的寬度,顯示在View的上部分位置 

FIT_XY / fitXY 把圖片不按比例擴大/縮小到View的大小顯示 

MATRIX / matrix 用矩陣來繪製,動態縮小放大圖片來顯示。 

** 要注意一點,Drawable文件夾裡面的圖片命名是不能大寫的。


相關焦點

  • android camera 沒圖像專題及常見問題 - CSDN
    ;import android.hardware.Camera;import android.os.Handler;import android.util.AttributeSet;import android.view.SurfaceHolder;import android.view.SurfaceView;import com.example.camera.util.CameraUtil;/*
  • kotlin-android-extensions插件也被廢棄了?扶我起來
    在開始介紹ViewBinding之前,我還是想先討論一下,為什麼kotlin-android-extensions插件會被廢棄。雖說Google的技術迭代頻率常常會讓我們直呼學不動了,但是Google也絕對不會無緣無故去廢棄一個之前主推的技術,說明kotlin-android-extensions插件肯定還是存在問題的。
  • 玩轉Android之加速度傳感器的使用,模仿微信搖一搖(一)
    1.布局文件好,那我們先來看看布局文件吧,在布局文件的正中央是一個花的圖片,上圖大家看到的手機圖片實際上是兩張圖片拼接在一起,將花的那張圖片遮住了,當搖一搖的時候,這兩張圖片分別向上或者向下移動,然後花的圖片就可以顯示出來。OK,基本原理就是這樣,我們來看看代碼: [java] view plain copy
  • Android平板多屏幕適配
    朱小姐 的博客地址:http://blog.csdn.net/qq_27570955這幾天做關於平板的項目,發現用之前的多屏幕適配方案,會直接運行閃退,報的錯誤是找不到指定的資源,克服了懶惰的心,翻牆查了api文檔。找到了問題。記錄下來。
  • android中表格布局的使用 - CSDN
    Android的三種布局使用線性布局,約束布局,表格布局實現三種頁面1.線性布局LinearLayout實現頁面中先採用LinearLayout的垂直布局,再在其中嵌套四個水平布局,每個水平LinearLayout中設計四個按鈕,分別命名為
  • android中的啟動模式 - CSDN
    Android總Activity的啟動模式分為四種: Activity啟動模式設置: <activity android:name=".MainActivity" android:launchMode="standard" />Activity的四種啟動模式: 1. standard 模式啟動模式,
  • 【android入門】開啟裝逼之旅
    Android入門指南哈哈,今天和大家聊一聊android入門哈。首先,你需要準備一兩本與android有關的、簡單的入門級書籍 。www.jikexueyuan.com/3、 麥子學院http://www.maiziedu.com/學習技術一定要多看看相關的博客1、 專注於開源項目源碼解析及優秀開源項目的分享http://a.codekk.com/2、 性能優化、源碼解析http://www.trinea.cn/3、 Android系統原始碼分析http://blog.csdn.net
  • Android 沉浸式狀態欄攻略 讓你的狀態欄變色吧
    三、實現半透明狀態欄因為本例使用了NavigationView,所以布局代碼稍多,當然如果你不需要,可以自己進行篩減。(二)布局文件activity_main.xml<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto
  • Android仿摩拜單車側滑菜單
    視覺效果:製作步驟:使用到了NavigationView,來自於design包,需添加依賴:‍ compile 'com.android.support:design:26.0.0-alpha1'添加主布局,在activity_main.xml中:DrawerLayout
  • android 服務啟動後專題及常見問題 - CSDN
    (ActivityManagerService.this) { mDidUpdate = true; } showBootMessage(mContext.getText( R.string.android_upgrading_complete
  • Android實現仿淘寶物流追蹤功能
    想了想,可以各個view的相對位置來實現啊,比如,那個圓點也可以用imageview實現啊,唯一不同的就是豎線的view是穿過灰色的圓view的,大家有沒有發現,除了第一個圓點以外的其他圓點,都是在那條水平的分割線下固定高度的位置(我這裡好像是10dp)的左側 ,那我完全可以在這個分割線下方弄一個空的View,那麼穿過圓孔的這條豎線不就是在這個空的View的上方,在圓點的下方,並且高度是match,
  • Android仿全歷史——全沉浸時間軸實現
    1.沉浸狀態欄與透明背景狀態欄的沉浸涉及到不同版本、不同情景狀態下的適配,相對來講比較複雜。想要研究的比較深的朋友,可以查看https://www.jianshu.com/p/dc20e98b9a90這篇博客,內容充實,講的也非常詳細,本文的的沉浸適配也對其進行了參考。我們今天涉及到的沉浸狀態欄實際非常簡單,準確的講應該叫透明狀態欄,也並不包含交互效果下狀態欄的變換。
  • Android劉海屏適配全方案(華為、小米、Vivo、Oppo)
    ,只包含一個按鈕,為了明顯,我為根布局設置了一個背景。1.沉浸式狀態欄的適配其實沉浸式狀態欄帶來的遮擋問題與劉海屏無關,本質上是由於設置了透明狀態欄導致布局延伸到了狀態欄中,就算是不具有劉海屏,一定程度上也會造成布局的遮擋。不過既然劉海屏是處在狀態欄當中的,那麼我們就把這種情況也包含在劉海屏的適配中。清楚了原因之後,解決起來就很簡單了,我們只需要讓控制項或布局避開狀態欄顯示就可以了,具體的解決方法有三種。
  • Android Material Design系列之Navigation Drawer
    DrawerLayout布局<?xml version="1.0" encoding="utf-8"?="@layout/nav_header_main"        app:menu="@menu/activity_main_drawer" /></android.support.v4.widget.DrawerLayout>從上面的布局代碼中我們就看出來了,DrawerLayout包含NavigationView,中間的include先不管,那是toolbar
  • Android 樣式系統 | 主題背景屬性
    這種方法的額外好處是,布局/樣式引用這些顏色時復用性變得更高。由於主題背景可以被覆蓋或者改變,因此這間接表示: 您不需要創建其他布局或樣式就可以更改某些顏色——您可以在相同的布局中使用不同的主題背景。在某些情況下,您或許不想按照主題背景更改顏色。例如,在 Material Design 規範文檔中提到,您可能希望在淺色和深色主題中均使用同一類型的顏色。
  • Android Design Support Library之TabLayout
    app/build.gradle 中添加下面依賴: compile 'com.android.support:design:25.3.1'<android.support.design.widget.TabLayout android:id="@+id/tabLayout" android:layout_width
  • Android實現界面切換時的共享動畫效果
    來看實現步驟這邊分為兩個界面:MainActivity 和 ActivityDetails,從 MainActivity 跳轉到 ActivityDetails首先來說 MainActivity:這邊的列表我是用的 RecyclerView 來實現的,用RecyclerView來顯示數據這個太簡單就不用說了吧,注意,這裡有個不同的地方在於在列表的 Item 布局文件中
  • Android10.0(Q) Launcher3 去掉抽屜效果!
    作者: cczhengv,連結:https://blog.csdn.net/u012932409/article/details/107483495效果圖修改思路1、增加變量 launcher3.is_full_app,用來動態切換4、將 AllAppsContainerView 中的圖標加載到 Workspace5、新安裝的
  • Android開發樣式和主題背景
    從 Android 5.0(API 級別 21)和 Android 支持庫 v22.1 開始,您還可以在布局文件中為視圖指定 android:theme 屬性。這會修改該視圖及任何子視圖的主題背景,適用於更改界面特定部分中的主題背景調色板。前面的示例展示了如何應用主題背景,例如 Android 支持庫提供的 Theme.AppCompat。
  • gradle 依賴衝突 - CSDN
    configurations.all { //強制使用某個版本的依賴,若需強制多個依賴,可以逗號分割, resolutionStrategy.force 'com.android.support