Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

如何動態改變 AbsoluteLayout 中其他子元件的座標位置

編輯:Android 開發實例

在使用 AbsoluteLayout(絕對定位)時,時常會遇到需要「執行期動態調整子元件位置」的需求。以下透過實例說明幾種做法與常見的陷阱。

先看一段典型的配置檔:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <AbsoluteLayout xmlns:android="https://schemas.android.com/apk/res/android"
  3. android:id="@+id/mArtivleViewLayout" android:layout_width="fill_parent"
  4. android:layout_height="fill_parent" android:background="@drawable/white"
  5. android:orientation="vertical">
  6. <LinearLayout android:layout_y="350px" android:layout_x="0px"
  7. android:layout_width="fill_parent" android:id="@+id/TooLBarLayout"
  8. android:layout_height="44px" android:gravity="center"
  9. android:background="@drawable/toolbarbg" android:visibility="visible">
  10. <ImageView android:id="@+id/toolbar_pageup"
  11. android:layout_height="wrap_content"
  12. android:layout_width="wrap_content" android:src="@drawable/toolbar_pageup">
  13. </ImageView>
  14. </LinearLayout>
  15. </AbsoluteLayout>

直覺上你可能會想用 offsetTopAndBottom、offsetLeftAndRight 來移動它:

  1. mLinearLayout = (LinearLayout) findViewById(R.id.TooLBarLayout);
  2. mLinearLayout.offsetTopAndBottom(50);
  3. mLinearLayout.offsetLeftAndRight(0);

但實際執行卻發現一點反應都沒有——畫面完全沒變。這正是使用 AbsoluteLayout 時常見的困惑。

要正確地動態改變位置,關鍵在於「重新設定 LayoutParams」。以下是一個完整可運作的寫法:

  1. package com.test;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.webkit.WebView;
  7. import android.widget.AbsoluteLayout;
  8. import android.widget.LinearLayout;
  9. public class abslayout extends Activity {
  10. private WebView mWebView;
  11. private LinearLayout linearLayout;
  12. public void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. this.setContentView(R.layout.abslayout);
  15. linearLayout = (LinearLayout) ((ViewGroup) findViewById(R.id.mArtivleViewLayout)).getChildAt(0);
  16. linearLayout.setLayoutParams(new AbsoluteLayout.LayoutParams(linearLayout.getWidth(), linearLayout.getHeight(),
  17. linearLayout.getLeft() + 5, linearLayout.getTop() + 5));
  18. }
  19. }

對應的配置檔如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <AbsoluteLayout
  3. xmlns:android="https://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent" android:id="@+id/mArtivleViewLayout"
  5. android:layout_height="fill_parent" android:background="#ffffff">
  6. <LinearLayout android:layout_y="250px" android:layout_x="0px"
  7. android:layout_width="320px" android:id="@+id/LinearLayout01"
  8. android:layout_height="50px" android:background="#000000">
  9. </LinearLayout>
  10. </AbsoluteLayout>

需要特別注意的是:如果在 onCreate 中直接呼叫 setLayoutParams 往往看不到效果,把它放進某個「觸發動作」裡(例如按鈕的點擊事件)就能正常生效:

  1. private class mPageUpOnClickListener implements OnClickListener {
  2. public void onClick(View v) {
  3. Log.v("AA","sfsdfsfsfsfs");
  4. linearLayout.setLayoutParams(new AbsoluteLayout.LayoutParams(
  5. linearLayout.getWidth(), linearLayout.getHeight(),
  6. linearLayout.getLeft() + 5, linearLayout.getTop() + 5));
  7. }
  8. }

如此一來,每次點擊都會重新套用新的 LayoutParams,元件便會依新的座標重新配置並顯示出來了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved