編輯:關於 Android 程式設計
每當我們看到 Android 手機上呈現出精緻漂亮的頁面時,總會好奇它是如何做出來的。其實並不難,因為每一個頁面背後都必定有一個對應的佈局。本文將介紹 Android 開發中最常使用的三種佈局:線性佈局、表格佈局與相對佈局。無論哪種佈局,通常都有 XML 與程式碼兩種實作方式。
線性佈局透過 orientation 屬性控制元件是水平還是垂直排列。以下是垂直排列的 XML 範例:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button android:id="@+id/testButton4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/myRalativeLayoutButton3"/>
</LinearLayout>
雖然開發中大量採用 XML 方式,但掌握程式碼生成也很重要。特別是當頁面上要顯示的元件並不固定、需要依據使用者操作動態呈現時,用程式碼建立會方便許多。以下以程式碼動態建立線性佈局,並用一個按鈕控制單選框的顯示與隱藏:
public class myLinearLayout extends Activity{
private boolean flag=true;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final LinearLayout layout=new LinearLayout(this);
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams textParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
TextView tv=new TextView(this);
tv.setLayoutParams(textParams);
tv.setText(R.string.myLinearLayoutText);
tv.setTextSize(20);
layout.addView(tv);
LinearLayout.LayoutParams buttonParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
Button button =new Button(this);
button.setLayoutParams(buttonParams);
button.setText(R.string.myLinearLayoutButton1);
layout.addView(button,buttonParams);
final RadioGroup radioGroup=new RadioGroup(myLinearLayout.this);
final RadioButton radioButton1=new RadioButton(myLinearLayout.this);
radioButton1.setId(1);
radioButton1.setText(R.string.maleText);
radioButton1.setChecked(true);
radioGroup.addView(radioButton1);
final RadioButton radioButton2=new RadioButton(myLinearLayout.this);
radioButton2.setId(2);
radioButton2.setText(R.string.famaleText);
radioGroup.addView(radioButton2);
layout.addView(radioGroup);
radioGroup.setVisibility(View.GONE);
super.setContentView(layout,layoutParams);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
if(flag){
radioGroup.setVisibility(View.VISIBLE);
flag=false;
}else{
radioGroup.setVisibility(View.GONE);
flag=true;
}
}
});
}
}
其中 GONE 與 VISIBLE 分別用來隱藏與顯示元件。
打開 Android 手機時,眼前呈現的往往就是一個表格佈局。以下用 XML 呈現一個包含表頭的表格:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/myTableLayoutId"
android:shrinkColumns="3"
android:background="@drawable/psu">
<TableRow>
<TextView android:layout_column="0" android:gravity="center_vertical" android:padding="8px" android:text="ID"/>
<TextView android:layout_column="1" android:gravity="center_vertical" android:padding="8px" android:text="name"/>
<TextView android:layout_column="2" android:gravity="center_vertical" android:padding="8px" android:text="email"/>
<TextView android:layout_column="3" android:padding="8px" android:gravity="center_vertical" android:text="address"/>
</TableRow>
</TableLayout>
表格佈局與 Java Web 中的 table 概念相似,其中的 TableRow 相當於 tr。shrinkColumns 屬性用來控制當某一行內容過長無法完整顯示時,指定哪一列可以換行壓縮。此外,還能透過 android:gravity 控制每個 TableRow 內容的置中、靠左或靠右等對齊方式。
以下以程式碼動態組裝表格,重點在於透過索引 j 控制元件確實落在同一列中:
public class myTableLayoutTwo extends Activity {
private Map<String, String> mapTitle = new HashMap<String, String>();
private List<Map<String, String>> listData = new ArrayList<Map<String, String>>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.mapTitle.put("id", "編號");
this.mapTitle.put("name", "姓名");
this.mapTitle.put("cj", "成績");
for (int i = 0; i < 3; i++) {
Map<String, String> map = new HashMap<String, String>();
map.put("id", String.valueOf(i));
map.put("name", "bruce" + i);
map.put("cj", String.valueOf(i));
this.listData.add(map);
}
TableLayout layout = new TableLayout(this);
TableLayout.LayoutParams tableLayout = new TableLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
layout.setBackgroundResource(R.drawable.psu);
TableRow tableRowTitle=new TableRow(this);
for(int i=0;i<this.mapTitle.size();i++){
String key="";
switch (i) {
case 0: key="id"; break;
case 1: key="name"; break;
case 2: key="cj"; break;
}
TextView textViewTitle=new TextView(this);
textViewTitle.setText(this.mapTitle.get(key));
tableRowTitle.addView(textViewTitle);
}
layout.addView(tableRowTitle);
for(int i=0;i<this.listData.size();i++){
Map<String,String> map=this.listData.get(i);
TableRow tableRow=new TableRow(this);
for(int j=0;j<map.size();j++){
String key="";
switch (j) {
case 0: key="id"; break;
case 1: key="name"; break;
case 2: key="cj"; break;
}
TextView textViewContent=new TextView(this);
textViewContent.setText(map.get(key));
tableRow.addView(textViewContent,j);
}
layout.addView(tableRow);
}
super.setContentView(layout,tableLayout);
}
}
顧名思義,相對佈局需要一個參照物,才能相對它來排列元件。以下範例將 id 為 imgb 的圖片放在 imga 的右邊,透過 toRightOf 屬性控制:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/myRelativeLayoutId"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:id="@+id/imga"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/android_mldn_01"/>
<ImageView android:id="@+id/imgb"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/android_mldn_02"
android:layout_toRightOf="@id/imga"/>
</RelativeLayout>
程式碼實作時,透過 addRule 方法設定相對關係,例如 RIGHT_OF、BELOW 等:
public class myRelativeLayout extends Activity {
private RelativeLayout myRelativeLayout;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.myralativelayout);
this.myRelativeLayout = (RelativeLayout) super.findViewById(R.id.myRelativeLayoutId);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.imga);
relativeLayoutParams.addRule(RelativeLayout.BELOW, R.id.myT1);
Button button=new Button(this);
button.setText(R.string.maleText);
this.myRelativeLayout.addView(button,relativeLayoutParams);
}
}
這裡要特別提醒:應避免在應用中出現硬編碼,像 button.setText("文字") 這種寫法最好改為在 string.xml 中定義,統一管理說明文字,例如 button.setText(R.string.maleText)。此外,由於手機的記憶體與 CPU 資源有限,開發時要權衡使用程式碼還是 XML 建立佈局,一般建議優先採用 XML。掌握以上三種佈局並靈活搭配巢狀使用,就能打造出幾乎所有想要的畫面效果。
Android 系统联系人全特效实现(上):分组导航与挤压动画(附源码)
记得刚接触 Android 时,我对系统联系人中的特效很感兴趣:它会根据手机中联系人姓氏的首字母进行分组,并在界面最顶端始终显示当前的分组。最让我着迷的是,当后一个分组和
Android 性能優化:告別卡頓的佈局與渲染優化
Android 應用出現卡頓,往往不是單一原因造成的。從渲染機制來看,佈局層級、過度繪製、自定義 View 的繪製方式、透明度處理等,都可能影響最終流暢度。本文從渲染相關
關於 ListView 的全面總結:適配器、原理與內存優化
網上關於 ListView 的介紹很多,我挑選了一些內容整合於此,僅供參考。 在 Android 開發中,ListView 是比較常用的組件。 官方 API 地址:htt
Android 如何實現投票/觀點表達界面:標籤流式佈局與狀態切換
先上效果圖: 這個界面有兩個核心技術點:一是把一堆「觀點標籤」按屏幕寬度自動換行排列;二是點擊後有按下態的視覺反饋。 一、容器:一個豎向 LinearLayout