android LinearLayout 布局實(shí)例代碼
<?xml version="1.0" encoding="utf-8"?>
<!--
<LinearLayout>
線性版面配置,在這個(gè)標(biāo)簽中,所有元件都是按由上到下的排隊(duì)排成的
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- android:orientation="vertical" 表示豎直方式對(duì)齊
android:orientation="horizontal"表示水平方式對(duì)齊
android:layout_width="fill_parent"定義當(dāng)前視圖在屏幕上
可以消費(fèi)的寬度,fill_parent即填充整個(gè)屏幕。
android:layout_height="wrap_content":隨著文字欄位的不同
而改變這個(gè)視圖的寬度或者高度。有點(diǎn)自動(dòng)設(shè)置框度或者高度的意思
layout_weight 用于給一個(gè)線性布局中的諸多視圖的重要度賦值。
所有的視圖都有一個(gè)layout_weight值,默認(rèn)為零,意思是需要顯示
多大的視圖就占據(jù)多大的屏幕空 間。若賦一個(gè)高于零的值,則將父視
圖中的可用空間分割,分割大小具體取決于每一個(gè)視圖的layout_weight
值以及該值在當(dāng)前屏幕布局的整體 layout_weight值和在其它視圖屏幕布
局的layout_weight值中所占的比率而定。
舉個(gè)例子:比如說(shuō)我們?cè)?水平方向上有一個(gè)文本標(biāo)簽和兩個(gè)文本編輯元素。
該文本標(biāo)簽并無(wú)指定layout_weight值,所以它將占據(jù)需要提供的最少空間。
如果兩個(gè)文本編輯元素每一個(gè)的layout_weight值都設(shè)置為1,則兩者平分
在父視圖布局剩余的寬度(因?yàn)槲覀兟暶鬟@兩者的重要度相等)。如果兩個(gè)
文本編輯元素其中第一個(gè)的layout_weight值設(shè)置為1,而第二個(gè)的設(shè)置為2,
則剩余空間的三分之二分給第一個(gè),三分之一分給第二個(gè)(數(shù)值越小,重要
度越高)。
-->
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="red"
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="green"
android:gravity="center_horizontal"
android:background="#00aa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="blue"
android:gravity="center_horizontal"
android:background="#0000aa"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<TextView
android:text="yellow"
android:gravity="center_horizontal"
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="2">
<TextView
android:text="row one"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row two"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row three"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:text="row four"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
感覺(jué)這種形式有點(diǎn)像div+css的方式布局,不過(guò)這種方式的靈活性和div+css還是有些不及,主要是那android:layout_weight的值如何去確定,而且采用的是數(shù)值越小,重要度越高的方式,分配起來(lái)還得好好計(jì)算一下。
Java代碼 Views.java
package com.cn.view;
import android.app.Activity;
import android.os.Bundle;
public class Views extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
- android LinearLayout和RelativeLayout組合實(shí)現(xiàn)精確布局方法介紹
- Android應(yīng)用借助LinearLayout實(shí)現(xiàn)垂直水平居中布局
- Android中LinearLayout布局的常用屬性總結(jié)
- android 線性布局LinearLayout實(shí)例代碼
- Android 布局控件之LinearLayout詳細(xì)介紹
- Android App中的多個(gè)LinearLayout嵌套布局實(shí)例解析
- Android自定義控件LinearLayout實(shí)例講解
- Android應(yīng)用的LinearLayout中嵌套R(shí)elativeLayout的布局用法
- Android App中使用LinearLayout進(jìn)行居中布局的實(shí)例講解
- Android使用LinearLayout設(shè)置邊框
相關(guān)文章
關(guān)于Android多渠道打包問(wèn)題看這一篇就夠了
這篇文章主要介紹了關(guān)于Android程序的多渠道打包方法,還不會(huì)的同學(xué)快進(jìn)來(lái)學(xué)習(xí)下吧,建議收藏以防迷路2021-08-08利用Warensoft Stock Service編寫高頻交易軟件
本文主要介紹了利用Warensoft Stock Service編寫高頻交易軟件的方法步驟,具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01淺析Android手機(jī)衛(wèi)士接收短信指令執(zhí)行相應(yīng)操作
通過(guò)廣播接收者,接收到短信,對(duì)短信內(nèi)容進(jìn)行判斷,如果為我們指定的值就執(zhí)行相應(yīng)的操作。本文給大家介紹Android手機(jī)衛(wèi)士接收短信指令執(zhí)行相應(yīng)操作,感興趣的朋友參考下吧2016-04-04Android App開(kāi)發(fā)中使用RecyclerView實(shí)現(xiàn)Gallery畫(huà)廊的實(shí)例
這篇文章主要介紹了Android App開(kāi)發(fā)中使用RecyclerView實(shí)現(xiàn)Gallery畫(huà)廊的實(shí)例,比普通的ListView實(shí)現(xiàn)的效果更為強(qiáng)大,需要的朋友可以參考下2016-04-04Android實(shí)現(xiàn)從本地圖庫(kù)/相機(jī)拍照后裁剪圖片并設(shè)置頭像
玩qq或者是微信的盆友都知道,這些聊天工具里都要設(shè)置頭像,一般情況下大家的解決辦法是從本地圖庫(kù)選擇圖片或是從相機(jī)拍照,然后根據(jù)自己的喜愛(ài)截取圖片,接下來(lái)通過(guò)本文給大家介紹Android實(shí)現(xiàn)從本地圖庫(kù)/相機(jī)拍照后裁剪圖片并設(shè)置頭像,需要的朋友參考下2016-02-02Android程序啟動(dòng)時(shí)出現(xiàn)黑屏問(wèn)題的解決方法
這篇文章主要介紹了Android程序啟動(dòng)時(shí)出現(xiàn)黑屏問(wèn)題的解決方法,分析了黑屏出現(xiàn)的原因及相應(yīng)的解決方法,需要的朋友可以參考下2016-08-08