想要实现一个效果:在ActivityGroup上有一固定的Gallery,子Activity在剩下的空间显示。在子Activity中有几个控件,第一行是一个TextView,第二行是ListView,最下面是并排显示的EditText和Button。要求是TextView和EditText、Button各自占用默认的控件,ListView完全占满剩下的空间。我写了布局文件,在预览的时候效果是可以的,但是在模拟器上跑起来,就走样了,listview在够多的时候是完全充满剩余空间,但是在为空或者只有1、2条左右的时候,就只占内容显示的那么一点空间,EditText和Button下面就是大片的空白。不知道问题出在哪,求指点。
下面是ActivityGroup和子Activity的布局文件:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <LinearLayout android:id="@+id/navilayout"
- android:layout_height="wrap_content" android:layout_width="fill_parent">
- <Gallery android:id="@+id/navigallery" android:layout_width="fill_parent" android:layout_height="wrap_content"></Gallery>
- </LinearLayout>
- <LinearLayout android:id="@+id/pagelayout" android:layout_weight="1"
- android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center_horizontal"></LinearLayout>
- </LinearLayout>
复制代码
- <?xml version="1.0" encoding="utf-8"?>
- <AbsoluteLayout android:layout_width="fill_parent"
- android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
- <LinearLayout android:id="@+id/comment_page_main"
- android:layout_width="fill_parent" android:layout_height="fill_parent"
- android:orientation="vertical" android:layout_x="0px"
- android:layout_y="0px">
- <LinearLayout android:id="@+id/comment_page_head"
- android:layout_width="fill_parent" android:layout_height="wrap_content"
- android:background="#ffffff00" android:orientation="horizontal"
- android:layout_gravity="center_horizontal">
- </LinearLayout>
- <LinearLayout android:id="@+id/comment_page_content"
- android:layout_width="fill_parent" android:layout_height="wrap_content"
- android:background="#ffff0033" android:orientation="vertical"
- android:layout_weight="1" android:layout_gravity="center_horizontal">
- <TextView android:id="@+id/tip" android:layout_width="wrap_content"
- android:layout_height="fill_parent">
- </TextView>
- <ListView android:id="@+id/comment_list"
- android:layout_width="fill_parent" android:layout_height="fill_parent">
- </ListView>
- </LinearLayout>
- <LinearLayout android:id="@+id/comment_page_bottom"
- android:layout_width="fill_parent" android:layout_height="wrap_content"
- android:background="#ff00cc99" android:orientation="vertical">
- <EditText android:id="@+id/comment_input"
- android:layout_width="fill_parent" android:layout_height="wrap_content"
- android:hint="@string/default_input" android:textSize="12sp"
- android:singleLine="false">
- </EditText>
- <Button android:id="@+id/comment_send" android:text="@string/send_comment"
- android:layout_width="wrap_content" android:layout_height="wrap_content">
- </Button>
- </LinearLayout>
- </LinearLayout>
- </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中则相反参数越大重要度越高,取值可以多个。
以前在别的帖子上看到过总结,通过自己测试还是理解最深刻。