編輯:關於 Android 程式設計
LinearLayout 是 Android 中最常用的布局之一,它以線性方式排列子元素,可以是垂直方向,也可以是水平方向。下面透過一份完整的布局代碼,配合詳盡註釋,幫助你理解 LinearLayout 的各項屬性與 layout_weight 的用法。
復制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<!--
<LinearLayout>
線性版面配置,在這個標簽中,所有元件都是按由上到下的排隊排成的
-->
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- android:orientation="vertical" 表示豎直方式對齊
android:orientation="horizontal"表示水平方式對齊
android:layout_width="fill_parent"定義當前視圖在屏幕上
可以消費的寬度,fill_parent即填充整個屏幕。
android:layout_height="wrap_content":隨著文字欄位的不同
而改變這個視圖的寬度或者高度。有點自動設置框度或者高度的意思
layout_weight 用於給一個線性布局中的諸多視圖的重要度賦值。
所有的視圖都有一個layout_weight值,默認為零,意思是需要顯示
多大的視圖就占據多大的屏幕空 間。若賦一個高於零的值,則將父視
圖中的可用空間分割,分割大小具體取決於每一個視圖的layout_weight
值以及該值在當前屏幕布局的整體 layout_weight值和在其它視圖屏幕布
局的layout_weight值中所占的比率而定。
舉個例子:比如說我們在 水平方向上有一個文本標簽和兩個文本編輯元素。
該文本標簽並無指定layout_weight值,所以它將占據需要提供的最少空間。
如果兩個文本編輯元素每一個的layout_weight值都設置為1,則兩者平分
在父視圖布局剩余的寬度(因為我們聲明這兩者的重要度相等)。如果兩個
文本編輯元素其中第一個的layout_weight值設置為1,而第二個的設置為2,
則剩余空間的三分之二分給第一個,三分之一分給第二個(數值越小,重要
度越高)。
-->
<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>
這種嵌套布局的方式有點像 Web 前端中 div+css 的思路,只不過靈活性略遜一籌,尤其是 android:layout_weight 的取值需要仔細權衡,而且採用「數值越小、重要度越高」的分配規則,佈局時還是需要好好計算一下比例。
對應的 Java 代碼 Views.java 如下:
復制代碼 代碼如下:
package com.cn.view;
import android.app.Activity;
import android.os.Bundle;
public class Views extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Android Activity 核心概念整理:生命週期、任務堆疊與 Intent 匹配
Activity 是 Android 四大元件之一,也是使用者最直接接觸到的應用程式介面。它不僅負責顯示畫面,還承載著各種 UI 元件,因此常被視為畫面與控制件的容器。
Android 讓文字與圖示同行顯示:文字超出省略、圖示自動靠右
一、需求場景 標題說得有點繞,直接看圖比較快:影片、票券之類的小圖示要緊跟在標題後方,一旦標題太長,圖示必須被擠到省略號「…」的後面,也就是文字截斷、圖示依然完整靠右。
Android 視頻播放器實作:豎屏播放界面
豎屏播放界面 播放視頻通常放在獨立 Activity。豎屏時,上半部播放影片,下半部顯示標題、解析度、檔案大小與建立時間等資訊。 啟動播放器 點擊列表項目時,可建立 I
監聽 ScrollView 滾動實現懸浮吸頂效果:重現美團、大眾點評的購買欄
行動裝置幾乎成了每個人的隨身配備,而決定一個 App 好不好的,除了功能本身,使用者體驗與介面細節往往才是關鍵。最近在美團與大眾點評上看到一個很貼心的設計:往下滑動時,「