編輯:關於 Android 程式設計
TableLayout 的模型很直白:把容器劃分成一列一列(行),每一行裡可以放多個控件,形成表格狀的排布。它還支援設置控件在行內的對齊方式,以及標記某一行是否為「可收縮」,讓內容過寬時能自動壓縮。
TableLayout 中的每一行由 TableRow 表示,未被子元素填滿的行不會自動補位。
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1">
<TableRow>
<TextView android:id="@+id/text01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用戶名" />
<EditText android:id="@+id/edit01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10">
<requestFocus />
</EditText>
</TableRow>
<TableRow>
<TextView android:id="@+id/text02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 碼" />
<EditText android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" />
</TableRow>
<TableRow>
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="登錄系統" />
</TableRow>
</TableLayout>
從運行效果可以清楚看到:第一行、第二行各自排了兩個控件,第三行只有一個按鈕,並透過 layout_gravity 達到了水平居中。
package com.example.tablelayout;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout layout = new TableLayout(this);
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
// 三行
TableRow row1 = new TableRow(this);
TableRow row2 = new TableRow(this);
TableRow row3 = new TableRow(this);
// 第一行:用戶名
TextView tv1 = new TextView(this);
tv1.setText("用戶名");
EditText et1 = new EditText(this);
row1.addView(tv1);
row1.addView(et1);
// 第二行:密碼
TextView tv2 = new TextView(this);
tv2.setText("密 碼");
EditText et2 = new EditText(this);
row2.addView(tv2);
row2.addView(et2);
// 第三行:居中按鈕
Button button = new Button(this);
button.setText("登錄系統");
row3.setGravity(Gravity.CENTER);
row3.addView(button);
layout.addView(row1);
layout.addView(row2);
layout.addView(row3);
setContentView(layout, tableParams);
}
}
動態布局能實現與 XML 完全一致的視覺效果,但代碼冗長、可讀性差,而且圖文混排時難以直觀調整。實務上的建議是:界面結構相對固定就用 XML,只有當行數、列數需要依數據動態變化時,才動用代碼生成。
另外兩個常用屬性值得記住:android:stretchColumns 指定哪些列可以拉伸填滿剩餘寬度,android:shrinkColumns 則指定哪些列在內容溢出時可以收縮。
下節預告:相對布局 RelativeLayout。
實作 Android 的 MVP 架構(搭配 RxJava)
內容大綱: Android 開發架構的選擇、如何一步步搭建分層架構、使用 RxJava 解決主線程發出網路請求的問題,以及結語。 一、Android 開發架構的選擇 原生
Android 專案怎麼改套件名稱?改包名讓多版本並存
做 Android 開發的人大概都遇過這種尷尬:同一支 App 的測試版和正式版套件名稱一樣,裝進手機就互相覆蓋,舊資料也跟著被洗掉。想讓不同版本在同一支機器上和平共處,
android-async-http 入門:異步網絡庫的特性、集成與核心用法
android-async-http 讓我們能輕鬆地異步獲取網絡數據或向服務器提交數據,接入成本極低。本文基於官方主頁 https://loopj.com/android
Android EditText 輸入框實作與事件處理
EditText 的角色EditText 是 Android 中最重要的使用者輸入控制項。除了使用 getText 取得內容,實務上常需要限制長度、過濾輸入類型,或提供提