當前位置:首頁 » 安卓系統 » layout方法android

layout方法android

發布時間: 2023-01-24 19:52:57

A. Android 自定義控制項 layout

Android 繪制流程

View :View主要執行layout方法,使用 serFrame 方法來設置本身 View 的四個頂點的位置,確定View本身的位置。
ViewGroup :ViewGroup主要執行onLayout方法,遞歸遍歷所有子View,確定子View的位置。

我們來看ViewRootImpl中的 performLayout() 方法

看到這里,那host.getMeasuredWidth() / host.getMeasuredHeight()是什麼?它是直接調用View中的方法,其實就是經過measure後的DecorView的測量寬度和高度。在 Android 自定義控制項 measure 中有說明。

2.3.2.1 我們先來看ViewGroup中的 layout() 方法

ViewGroup裡面的layout最終會調入到父類View中的layout,View的layout後面講解。這里可以先告訴大家,最終會調用View的onLayout方法,而ViewGroup的onLayout是抽象方法,所以它的子類LinearLayout必須要實現。

2.3.2.2 我們再來看LinearLayout中的 onLayout() 方法。

2.3.2.3 挑一個縱向的吧,我們再來看LinearLayout中的 layoutVertical() 方法。

2.3.2.4 我們再來看LinearLayout中的 setChildFrame() 方法。

又一次回到了View的layout方法,接下來就看View分發的layout。

我們先來看View中的 layout() 方法。

我們先來看View中的 onLayout() 方法。

空空如也,其實View的布局由父容器決定,所以空實現是正常的,當然也可以在自定義View中進行更改。

《Android 視圖模塊 全家桶》

Android開發之自定義控制項(二)---onLayout詳解
自定義View Layout過程 - 最易懂的自定義View原理系列(3)

B. Android layout(int ,int,int,int)四個參數是如何確定一個view的位置的

ViewGroup.layout(int l, int t, int r, int b)這個方法是確定View的大小和位置的,然後將其繪制出來,裡面的四個參數分別是View的四個點的坐標,它的坐標不是相對屏幕的原點,而且相對於它的父布局來說的。

如圖所示:

l 和 t 是控制項左邊緣和上邊緣相對於父類控制項左邊緣和上邊緣的距離。

r 和 b是空間右邊緣和下邊緣相對於父類控制項左邊緣和上邊緣的距離。

C. android中怎樣layout的引用

可以的 1.先找到layout文件對象 LayoutInflater inflater = LayoutInflater.from(this); View view=inflater.inflate(R.layout.ID, null); 2.再使用此對象找到其裡面的控制項 Button btn = (Button)view.findViewById(R.id.ok);

D. Android 自定義View之Layout過程

系列文章:

在上篇文章: Android 自定義View之Measure過程 ,我們分析了Measure過程,本次將會掀開承上啟下的Layout過程神秘面紗,
通過本篇文章,你將了解到:

在上篇文章的比喻里,我們說過:

該ViewGroup 重寫了onMeasure(xx)和onLayout(xx)方法:

同時,當layout 執行結束,清除PFLAG_FORCE_LAYOUT標記,該標記會影響Measure過程是否需要執行onMeasure。

該View 重寫了onMeasure(xx)和onLayout(xx)方法:

MyViewGroup里添加了MyView、Button兩個控制項,最終運行的效果如下:

可以看出,MyViewGroup 里子布局的是橫向擺放的。我們重點關注Layout過程。實際上,MyViewGroup里我們只重寫了onLayout(xx)方法,MyView也是重寫了onLayout(xx)方法。
接下來,分析View Layout過程。

與Measure過程類似,連接ViewGroup onLayout(xx)和View onLayout(xx)之間的橋梁是View layout(xx)。

可以看出,最終都調用了setFrame(xx)方法。

對於Measure過程在onMeasure(xx)里記錄了尺寸的值,而對於Layout過程則在layout(xx)里記錄了坐標值,具體來說是在setFrame(xx)里,該方法兩個重點地方:

View.onLayout(xx)是空實現
從layout(xx)和onLayout(xx)聲明可知,這兩個方法都是可以被重寫的,接下來看看ViewGroup是否重寫了它們。

ViewGroup.layout(xx)雖然重寫了layout(xx),但是僅僅做了簡單判斷,最後還是調用了View.layout(xx)。

這重寫後將onLayout變為抽象方法,也就是說繼承自ViewGroup的類必須重寫onLayout(xx)方法。
我們以FrameLayout為例,分析其onLayout(xx)做了什麼。

FrameLayout.onLayout(xx)為子布局Layout的時候,起始坐標都是以FrameLayout為基準,並沒有記錄上一個子布局佔了哪塊位置,因此子布局的擺放位置可能會重疊,這也是FrameLayout布局特性的由來。而我們之前的Demo在水平方向上記錄了上一個子布局的擺放位置,下一個擺放時只能在它之後,因此就形成了水平擺放的功能。
由此類推,我們常說的某個子布局在父布局裡的哪個位置,決定這個位置的即是ViewGroup.onLayout(xx)。

上邊我們分析了View.layout(xx)、View.onLayout(xx)、ViewGroup.layout(xx)、ViewGroup.onLayout(xx),這四者什麼關系呢?
View.layout(xx)

View.onLayout(xx)

ViewGroup.layout(xx)

ViewGroup.onLayout(xx)

View/ViewGroup 子類需要重寫哪些方法:

用圖表示:

通過上述的描述,我們發現Measure過程和Layout過程里定義的方法比較類似:

它倆的套路比較類似:measure(xx)、layout(xx)一般不需要我們重寫,measure(xx)里調用onMeasure(xx),layout(xx)為調用者設置坐標值。
若是ViewGroup:onMeasure(xx)里遍歷子布局,並測量每個子布局,最後將結果匯總,設置自己測量的尺寸;onLayout(xx)里遍歷子布局,並設置每個子布局的坐標。
若是View:onMeasure(xx)則測量自身,並存儲測量尺寸;onLayout(xx)不需要做什麼。

Measure過程雖然比Layout過程復雜,但仔細分析後就會發現其本質就是為了設置兩個成員變數:

而Layout過程雖然比較簡單,其本質是為了設置坐標值

將Measure設置的變數和Layout設置的變數聯系起來:

此外,Measure過程通過設置PFLAG_LAYOUT_REQUIRED 標記來告訴需要進行onLayout,而Layout過程通過清除 PFLAG_FORCE_LAYOUT來告訴Measure過程不需要執行onMeasure了。
這就是Layout的承上作用

我們知道View的繪制需要依靠Canvas繪制,而Canvas是有作用區域限制的。例如我們使用:

Cavas繪制的起點是哪呢?
對於硬體繪制加速來說:正是通過Layout過程中設置的RenderNode坐標。
而對於軟體繪制來說:

關於硬體繪制加速/軟體繪制 後續文章會分析。

這就是Layout的啟下作用
以上即是Measure、Layout、Draw三者的內在聯系。
當然Layout的"承上"還需要考慮margin、gravity等參數的影響。具體用法參見最開始的Demo。

getMeasuredWidth()/getMeasuredHeight 與 getWidth/getHeight區別
我們以獲取width為例,分別來看看其方法:

getMeasuredWidth():獲取測量的寬,屬於"臨時值"
getWidth():獲取View真實的寬
在Layout過程之前,getWidth() 默認為0
何時可以獲取真實的寬、高

下篇將分析Draw()過程,我們將分析"一切都是draw出來的"道理

本篇基於 Android 10.0

E. 求助:android:layout

布局的介紹:
1、在4.0以前版本中一共有五種布局,都是ViewGroup的子類。分別是AbsoluteLayout、RelativeLayout、LinearLayout、FrameLayout、TableLayout。而TableLayout是LinearLayout的子類。(中文分別是:絕對布局、相對布局、線性布局、幀布局、表格布局)。
2、在2.2操作系統中將AbsoluteLayout過期。而目前TableLayout也逐漸少用。
3、在4.0之後又新增GridLayout。(GridLayout最經典的案例是計算器界面)
總之,Android中一共有六種布局。目前推薦使用RelativeLayout、LinearLayout、GridLayout三種布局。

LinearLayout:
(一)、概念:線性布局控制其中的控制項或組件橫向或縱向排列。不線性布局布局中,每一行或每一列只能放一個控制項。並且線性布局不會換行。當控制項排列到窗體邊緣,後面的控制項就被隱藏,而不會顯示出來。
線性布局的默認方向是水平方向(Horizontal)。vertical

(二)、LinearLayout的常用屬性:
1.android:orientation 定義布局內控制項或組件的排列方式
可選項:vertical 、 horizontal

2.android:layout_width 定義控制項的寬度
可選項:fill_parent / match_parent/ wrap_content/絕對數值
備註:fill_parent / match_parent的效果完全一致,都是填充整個父控制項。但是自2.2版本開始推薦使用match_parent 。wrap_content指的是該控制項的寬度正好包裹內容物。

3.android:layout_height 定義控制項的高度
可選項:fill_parent / match_parent/ wrap_content/絕對數值
備註:fill_parent / match_parent的效果完全一致,都是高度填充整個父控制項。wrap_content指的是該控制項的高度正好包裹內容物。

4.android:id 設置控制項的id。這樣就可以在R.java中自動生成相應的值,在程序中通過findViewById就可以調用。
設置id的格式為:android:id = "@+id/id的名字"

5.android:background 設置控制項的背景顏色或背景圖片
例如:android:background="#ffffff"
android:background="@drawable/圖片名稱"
【備註:】
顏色有RGB顏色格式和ARGB格式。RGB是紅綠藍三原色。而ARGB是帶alpha的三原色,即有透明度的三原色。
#FFFFFF 代表白色
#000000 黑色
#FFFFFFFF 完全不透明
#00FFFFFF 完全透明
#88FFFFFF 半透明

6.android:layout_weight 設置控制項的權重。即各控制項在水平或者垂直方向上平均分配。
備註:如果是水平方向設置權重,要將android:layout_width設置為0dp,如果是垂直方向上使用權重,要將android:layout_height設置為0dp。否則權重容易受到高度或寬度的干擾而出現偏差。

7.android:gravity 該屬性用來控制該View的內容物的位置。
如果該屬性是定義在布局節點中,則該布局中所有控制項的位置都受到這個屬性的控制。
如果該屬性出現在Button、TextView、EditText等控制項中,則用來控制這些控制項上的文字的位置。
可選項有:top、bottom、left、right、center_vertical、fill_vertical 、 center、fill等等。
【備註:】本屬性與android:layout_gravity不同。

8.android:layout_gravity 該屬性用於設置控制項相對於容器的對齊方式。
可選項有:top、bottom、left、right、center_vertical、center_horizontal 、fill_vertical 、 center、fill等等。
這些可選項中不是適用於每一種布局。在垂直線性布局中,android:gravity為bottom不起作用;而水平線性布局中,android:gravity為right不起作用。
【備註:】而本屬性是android:layout_gravity屬性,與 android:gravity 屬性不同。
(三)、LinearLayout的特有屬性:【重新歸納:去除公共屬性後的特有屬性】
1、android:orientation 布局管理器內組件的排列方式
2、android:gravity 設置布局管理器內組件的對齊方式
(四)、 LinearLayout 子元素的特有屬性:
1、android:layout_weight 子元素在 LinearLayout 中所佔的權重
2、android:layout_gravity 子元素在 LinearLayout 中的對齊方式

RelativeLayout:
(一)、概念:指按著控制項之間的相對位置來進行布局。
(二)、RelativeLayout特有屬性:
1、android:gravity 設置布局容器內子控制項的對齊方式
2、android:ignoreGravity 設置布局管理器內哪個控制項不受gravity屬性的影響
(三)、RelativeLayout子元素的特有屬性:LayoutParams
A、第一組:指兄弟控制項之間的相對位置。該組屬性的值是另一個控制項的id。
layout_toRightOf 該控制項在哪個控制項的右側
layout_toLeftOf 該控制項在哪個控制項的左側
layout_above 該控制項在哪個控制項的上側
layout_below 該控制項在哪個控制項的下側
B、第二組:指兄弟控制項之間的對齊關系。該組屬性的值是另一個控制項的id。
layout_alignRight 該控制項與哪個控制項的右對齊
layout_alignLeft 該控制項與哪個控制項的左對齊
layout_alignTop 該控制項與哪個控制項的頂對齊
layout_alignBottom 該控制項與哪個控制項的底對齊
C、第三組:指控制項與父布局之間的對齊關系。該組屬性的值是true或者false。
layout_alignParentRight 該控制項與父布局控制項的右對齊嗎?
layout_alignParentLeft 該控制項與父布局控制項的左對齊嗎?
layout_alignParentTop 該控制項與父布局控制項的頂端對齊嗎?
layout_alignParentBottom 該控制項與父布局控制項的底部對齊嗎?
layout_centerInParent 該控制項位於父布局控制項的中心位置嗎?
layout_centerVertical 該控制項位於父布局控制項的垂直中心位置嗎?
layout_centerHorizontal 該控制項位於父布局控制項的水平中心位置嗎?

GridLayout:網格布局
(一)、GridLayout布局屬性:
1、android:alignmentMode
屬性說明:設置布局的對齊模式。可以取以下值:
alignBounds -- 對齊子視圖邊界。
alignMargins -- 對齊子視圖邊距。
2、android:columnCount
屬性說明:GridLayout的最大列數
3、android:rowCount
屬性說明:GridLayout的最大行數
4、android:orientation
屬性說明:GridLayout中子元素的布局方向。有以下取值:
horizontal -- 水平布局。
vertical -- 豎直布局。
5、android:columnOrderPreserved
屬性說明: 設置該網格布局是否保留列序號。默認是true。
6、android:rowOrderPreserved
屬性說明: 設置該網格布局是否保留行序號。默認是true。
7、android:useDefaultMargins
屬性說明: 設置GridLayout使用默認的邊距。默認值是false。
(二)、GridLayout子元素的屬性:
1、android:layout_column
屬性說明: 顯示該控制項的列。例如,android:layout_column="0",表示在第1列顯示該控制項;android:layout_column="1",表示在第2列顯示該控制項。
2、android:layout_row
屬性說明: 該控制項所在行。例如,android:layout_row="0",表示在第1行顯示該控制項;android:layout_row="1",表示在第2行顯示該控制項。它和 android:layout_column類似。
3、android:layout_columnSpan
屬性說明: 列合並。即該控制項所佔的列數。例如,android:layout_columnSpan="2",表示該控制項佔2列。

4、android:layout_rowSpan
屬性說明: 行合並。即該控制項所佔的行數。例如,android:layout_rowSpan="2",表示該控制項佔2行。
5、android:layout_gravity
屬性說明:該控制項的布局方式。選項值:
top -- 控制項置於容器頂部,不改變控制項的大小。
bottom -- 控制項置於容器底部,不改變控制項的大小。
left -- 控制項置於容器左邊,不改變控制項的大小。
right -- 控制項置於容器右邊,不改變控制項的大小。
center_vertical -- 控制項置於容器豎直方向中間,不改變控制項的大小。
fill_vertical -- 如果需要,則往豎直方向延伸該控制項。
center_horizontal -- 控制項置於容器水平方向中間,不改變控制項的大小。
fill_horizontal -- 如果需要,則往水平方向延伸該控制項。
center -- 控制項置於容器中間,不改變控制項的大小。
fill -- 如果需要,則往水平、豎直方向延伸該控制項。
clip_vertical -- 垂直剪切,剪切的方向基於該控制項的top/bottom布局屬性。若該控制項的gravity是豎直的:若它的gravity是top的話,則剪切該控制項的底部;若該控制項的gravity是bottom的,則剪切該控制項的頂部。
clip_horizontal -- 水平剪切,剪切的方向基於該控制項的left/right布局屬性。若該控制項的gravity是水平的:若它的gravity是left的話,則剪切該控制項的右邊;若該控制項的gravity是 right的,則剪切該控制項的左邊。
start -- 控制項置於容器的起始處,不改變控制項的大小。
end -- 控制項置於容器的結束處,不改變控制項的大小。

F. Android自定義layout怎麼寫

LinearLayout自定義方法有多種:
1、自定義xml布局,然後載入布局,自定義一個View繼承LinearLayout
2、在自定義控制項中聲明它的所有子元素,然後在Layout文件中像使用LinearLayout一樣去進行布局。

第二種比較煩 ,它需要在Layout文件中定義好子元素之後,要在代碼 onFinishInflate() 進行匹配子元素。

我就說說載入布局文件的方法吧。
首先:定義好layout文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="5dip"
android:paddingLeft="40dip"
android:paddingTop="5dip"
android:src="@drawable/right_icon" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dip"
android:text="主題"
android:textColor="#000000" />
<LinearLayout
android:layout_width="100dp"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="5dip"
android:paddingLeft="12dip"
android:paddingTop="5dip"
android:src="@drawable/home_icon" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="5dip"
android:paddingLeft="12dip"
android:paddingTop="5dip"
android:src="@drawable/add_icon" />
</LinearLayout>

</LinearLayout>
public class MyLinearLayout extends LinearLayout {

private ImageView imageView,iv_home,iv_add;
private TextView textView;

public MyLinearLayout (Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyLinearLayout (Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.actionbar, this);
imageView=(ImageView) findViewById(R.id.imageView1);
iv_home=(ImageView) findViewById(R.id.imageView2);
iv_add=(ImageView) findViewById(R.id.imageView3);
textView=(TextView)findViewById(R.id.textView1);

}

/**
* 設置圖片資源
*/
public void setImageResource(int resId) {
imageView.setImageResource(resId);
}

/**
* 設置顯示的文字
*/
public void setTextViewText(String text) {
textView.setText(text);
}

}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<cn.com.demo.view.MyLinearLayout
android:id="@+id/ll_actionbar"
android:layout_height="fill_parent<span style="font-family: Tahoma, 'Microsoft Yahei', Simsun;">" </span>
android:layout_width="wrap_content"
android:background="@drawable/bg"
/>
</LinearLayout>
接下來自定義一個MyLinearLayout繼承LinearLayout,並且載入剛剛寫好的layout文件。(比如http://www.tiecou.com)
public class MyLinearLayout extends LinearLayout {
private ImageView imageView,iv_home,iv_add;
private TextView textView;
public MyLinearLayout (Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyLinearLayout (Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.actionbar, this);
imageView=(ImageView) findViewById(R.id.imageView1);
iv_home=(ImageView) findViewById(R.id.imageView2);
iv_add=(ImageView) findViewById(R.id.imageView3);
textView=(TextView)findViewById(R.id.textView1);
}
/**
* 設置圖片資源
*/
public void setImageResource(int resId) {
imageView.setImageResource(resId);
}

/**
* 設置顯示的文字
*/
public void setTextViewText(String text) {
textView.setText(text);
}
}
最後,要的時候使用定義好的MyLinearLayout控制項。

熱點內容
U盤超級加密2008 發布:2025-05-19 19:44:32 瀏覽:453
燈帶編程軟體 發布:2025-05-19 19:32:30 瀏覽:285
如何判斷伺服器被多少人訪問 發布:2025-05-19 19:27:45 瀏覽:123
編程stata 發布:2025-05-19 19:12:18 瀏覽:513
解壓命令gz 發布:2025-05-19 19:11:37 瀏覽:823
linux下的程序開發 發布:2025-05-19 18:55:02 瀏覽:927
該文件夾未包含 發布:2025-05-19 18:54:17 瀏覽:195
安卓拳皇對戰用哪個平台 發布:2025-05-19 18:42:39 瀏覽:531
華為暢玩5怎麼取消鎖屏密碼 發布:2025-05-19 18:42:38 瀏覽:583
linuxrm文件夾 發布:2025-05-19 18:40:25 瀏覽:973