編輯:Android 開發實例
在使用 AbsoluteLayout(絕對定位)時,時常會遇到需要「執行期動態調整子元件位置」的需求。以下透過實例說明幾種做法與常見的陷阱。
先看一段典型的配置檔:
- <?xml version="1.0" encoding="utf-8"?>
- <AbsoluteLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:id="@+id/mArtivleViewLayout" android:layout_width="fill_parent"
- android:layout_height="fill_parent" android:background="@drawable/white"
- android:orientation="vertical">
- <LinearLayout android:layout_y="350px" android:layout_x="0px"
- android:layout_width="fill_parent" android:id="@+id/TooLBarLayout"
- android:layout_height="44px" android:gravity="center"
- android:background="@drawable/toolbarbg" android:visibility="visible">
- <ImageView android:id="@+id/toolbar_pageup"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content" android:src="@drawable/toolbar_pageup">
- </ImageView>
- </LinearLayout>
- </AbsoluteLayout>
直覺上你可能會想用 offsetTopAndBottom、offsetLeftAndRight 來移動它:
- mLinearLayout = (LinearLayout) findViewById(R.id.TooLBarLayout);
- mLinearLayout.offsetTopAndBottom(50);
- mLinearLayout.offsetLeftAndRight(0);
但實際執行卻發現一點反應都沒有——畫面完全沒變。這正是使用 AbsoluteLayout 時常見的困惑。
要正確地動態改變位置,關鍵在於「重新設定 LayoutParams」。以下是一個完整可運作的寫法:
- package com.test;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.ViewGroup;
- import android.webkit.WebView;
- import android.widget.AbsoluteLayout;
- import android.widget.LinearLayout;
- public class abslayout extends Activity {
- private WebView mWebView;
- private LinearLayout linearLayout;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.setContentView(R.layout.abslayout);
- linearLayout = (LinearLayout) ((ViewGroup) findViewById(R.id.mArtivleViewLayout)).getChildAt(0);
- linearLayout.setLayoutParams(new AbsoluteLayout.LayoutParams(linearLayout.getWidth(), linearLayout.getHeight(),
- linearLayout.getLeft() + 5, linearLayout.getTop() + 5));
- }
- }
對應的配置檔如下:
- <?xml version="1.0" encoding="utf-8"?>
- <AbsoluteLayout
- xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent" android:id="@+id/mArtivleViewLayout"
- android:layout_height="fill_parent" android:background="#ffffff">
- <LinearLayout android:layout_y="250px" android:layout_x="0px"
- android:layout_width="320px" android:id="@+id/LinearLayout01"
- android:layout_height="50px" android:background="#000000">
- </LinearLayout>
- </AbsoluteLayout>
需要特別注意的是:如果在 onCreate 中直接呼叫 setLayoutParams 往往看不到效果,把它放進某個「觸發動作」裡(例如按鈕的點擊事件)就能正常生效:
- private class mPageUpOnClickListener implements OnClickListener {
- public void onClick(View v) {
- Log.v("AA","sfsdfsfsfsfs");
- linearLayout.setLayoutParams(new AbsoluteLayout.LayoutParams(
- linearLayout.getWidth(), linearLayout.getHeight(),
- linearLayout.getLeft() + 5, linearLayout.getTop() + 5));
- }
- }
如此一來,每次點擊都會重新套用新的 LayoutParams,元件便會依新的座標重新配置並顯示出來了。
Android 頂部導航菜單左右滑動的兩種實作方案
本文示範在 Android 中實作頂部導航菜單的左右滑動效果,提供兩種方案:以 ViewPager 分頁切換,以及以 HorizontalScrollView 做連續滑動
Android ListView 非同步載入圖片實作:快取、視圖複用與標籤比對
只要列表裡的圖片來自網路,就該用非同步載入。理由很直接:讓使用者盯著空白框等圖片載完,是種刑罰。下面把整個實作拆成四個部分,並說明其中幾個非做不可的細節。一、非同步載入的
Android 本地化實作:從 values-it 到語言碼對照表的完整整理
Android 應用程式可能在許多不同地區的許多裝置上執行。為了讓應用程式更具互動性、也更貼近使用者,App 應該能處理符合當地語言環境的文字、數字與檔案格式。本章要解釋
Android 登入畫面教學:帳密欄位設計與三次錯誤後鎖定的實作
登入畫面是使用者進入 Facebook、微博這類服務前的第一道門。它要收集一組憑證,驗證通過就放行。聽起來平凡,但實作時有三個細節決定成品是否堪用:密碼欄有沒有遮好、錯誤