[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中则相反参数越大重要度越高,取值可以多个。

以前在别的帖子上看到过总结,通过自己测试还是理解最深刻。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值