天天看點

[Android UI界面] 用layout_weight實作ActivityGroup子Activity下剩餘空間自動填充問題

想要實作一個效果:在ActivityGroup上有一固定的Gallery,子Activity在剩下的空間顯示。在子Activity中有幾個控件,第一行是一個TextView,第二行是ListView,最下面是并排顯示的EditText和Button。要求是TextView和EditText、Button各自占用預設的控件,ListView完全占滿剩下的空間。我寫了布局檔案,在預覽的時候效果是可以的,但是在模拟器上跑起來,就走樣了,listview在夠多的時候是完全充滿剩餘空間,但是在為空或者隻有1、2條左右的時候,就隻占内容顯示的那麼一點空間,EditText和Button下面就是大片的空白。不知道問題出在哪,求指點。

下面是ActivityGroup和子Activity的布局檔案:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:orientation="vertical" android:layout_width="fill_parent"
  4.         android:layout_height="fill_parent">
  5.         <LinearLayout android:id="@+id/navilayout"
  6.                 android:layout_height="wrap_content" android:layout_width="fill_parent">
  7.                 <Gallery android:id="@+id/navigallery" android:layout_width="fill_parent" android:layout_height="wrap_content"></Gallery>
  8.         </LinearLayout>
  9.         <LinearLayout android:id="@+id/pagelayout" android:layout_weight="1"
  10.                 android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center_horizontal"></LinearLayout>
  11. </LinearLayout>

複制代碼

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <AbsoluteLayout android:layout_width="fill_parent"
  3.         android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
  4.         <LinearLayout android:id="@+id/comment_page_main"
  5.                 android:layout_width="fill_parent" android:layout_height="fill_parent"
  6.                 android:orientation="vertical" android:layout_x="0px"
  7.                 android:layout_y="0px">
  8.                 <LinearLayout android:id="@+id/comment_page_head"
  9.                         android:layout_width="fill_parent" android:layout_height="wrap_content"
  10.                         android:background="#ffffff00" android:orientation="horizontal"
  11.                         android:layout_gravity="center_horizontal">
  12.                 </LinearLayout>
  13.                 <LinearLayout android:id="@+id/comment_page_content"
  14.                         android:layout_width="fill_parent" android:layout_height="wrap_content"
  15.                         android:background="#ffff0033" android:orientation="vertical"
  16.                         android:layout_weight="1" android:layout_gravity="center_horizontal">
  17.                         <TextView android:id="@+id/tip" android:layout_width="wrap_content"
  18.                                 android:layout_height="fill_parent">
  19.                         </TextView>
  20.                         <ListView android:id="@+id/comment_list"
  21.                                 android:layout_width="fill_parent" android:layout_height="fill_parent">
  22.                         </ListView>
  23.                 </LinearLayout>
  24.                 <LinearLayout android:id="@+id/comment_page_bottom"
  25.                         android:layout_width="fill_parent" android:layout_height="wrap_content"
  26.                         android:background="#ff00cc99" android:orientation="vertical">
  27.                         <EditText android:id="@+id/comment_input"
  28.                                 android:layout_width="fill_parent" android:layout_height="wrap_content"
  29.                                 android:hint="@string/default_input" android:textSize="12sp"
  30.                                 android:singleLine="false">
  31.                         </EditText>
  32.                         <Button android:id="@+id/comment_send" android:text="@string/send_comment"
  33.                                 android:layout_width="wrap_content" android:layout_height="wrap_content">
  34.                         </Button>
  35.                 </LinearLayout>
  36.         </LinearLayout>
  37. </AbsoluteLayout>

複制代碼

通過N多個例子顯示來看,單獨的activity下的layout_weight設定顯示是預期效果,如果放在ActivityGroup下,view和layout的layout_weight均被當做預設值來處理。:@:@:@

TabActivity下測試完全正常。沒頭的蒼蠅啊,為何這個ActivityGroup如此特殊

經過小強般頑強不懈的鬥争,問題終于被自己找出來了。是ActivityGroup下:

Window window=getLocalActivityManager().startActivity("Activity",intent);

View view = subActivity.getDecorView();

LayoutParams param = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);

view.setLayoutParams(param);

container.addView(view);

高亮部分代碼沒有加進去。

經過如此這番,總算是對layout_weight在layout和在view中的特點銘刻于心。結貼自勉。。。

經過測試發現:

在layout中聲明android:layout_weight參數值越小重要度越高,占有的相對顯示區是越大的,取值好像隻能存在2個。比如有分别取值1、2、3的3個linearlayout,則值為3的layout直接不會在螢幕上顯示了。同時,如果有layout取值是0,則好像是直接覆寫其他的層,獨占父級層。

在view中則相反參數越大重要度越高,取值可以多個。

以前在别的文章上看到過總結,通過自己測試還是了解最深刻。

繼續閱讀