[Android1.6]动态添加View的问题
前言
小米加步枪,跑步进Android,还没来得及学习就直接项目了 - - # ,布局这块仍然是很麻烦的一块,先记录些问题吧。
正文
一、需求
动态切换View,即把grid.xml中的GridView动态添加到id为ContentView的LinearLayout中。
二、问题代码
2.1 main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:verticalSpacing="0dp"
android:padding="0dp" android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:layout_height="333dp" android:id="@+id/ContentView"
android:background="#FF0000" android:gravity="fill"
android:layout_width="fill_parent"></LinearLayout>
<!-- 底部菜单栏 -->
<LinearLayout
android:background="#000928" android:layout_width="wrap_content"
android:layout_height="wrap_content" androidrientation="horizontal" android:gravity="fill">
<ImageView android:src="@drawable/main_info"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ImageView android:src="@drawable/main_index"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ImageView android:src="@drawable/main_own"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ImageView android:src="@drawable/main_help"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ImageView android:src="@drawable/main_set"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ImageView android:src="@drawable/main_quit"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ImageView android:src="@drawable/main_return"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:verticalSpacing="0dp"
android:padding="0dp" android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:layout_height="333dp" android:id="@+id/ContentView"
android:background="#FF0000" android:gravity="fill"
android:layout_width="fill_parent"></LinearLayout>
<!-- 底部菜单栏 -->
<LinearLayout
android:background="#000928" android:layout_width="wrap_content"
android:layout_height="wrap_content" androidrientation="horizontal" android:gravity="fill">
<ImageView android:src="@drawable/main_info"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ImageView android:src="@drawable/main_index"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ImageView android:src="@drawable/main_own"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ImageView android:src="@drawable/main_help"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ImageView android:src="@drawable/main_set"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ImageView android:src="@drawable/main_quit"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<ImageView android:src="@drawable/main_return"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
2.2 grid.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="5"
android:verticalSpacing="30dp" android:horizontalSpacing="10dp"
android:columnWidth="90dp" android:stretchMode="columnWidth"
android:background="#000928" android:scrollY = "-50dp"
android:gravity="center"
/>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="5"
android:verticalSpacing="30dp" android:horizontalSpacing="10dp"
android:columnWidth="90dp" android:stretchMode="columnWidth"
android:background="#000928" android:scrollY = "-50dp"
android:gravity="center"
/>
2.3 后台代码
private void ChangeView()
{
ly.removeAllViews();
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.grid,null);
GridView gridview = (GridView)layout.findViewById(R.id.gridview);
gridview.setAdapter(new ItemAdapter(MainActivity.this));
gridview.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
ly.addView(gridview);
}
{
ly.removeAllViews();
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.grid,null);
GridView gridview = (GridView)layout.findViewById(R.id.gridview);
gridview.setAdapter(new ItemAdapter(MainActivity.this));
gridview.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
ly.addView(gridview);
}
代码说明:
a). ly为main.xml中id为ContentView的LinearLayout,即需动态添加View的容器
b). ItemAdapter为Grid填充数据的辅助类
2.4 现象
2.4.1 正常
如果把grid.xml中GridView的代码直接复制粘贴到main.xml中LinearLayout容器内没有任何问题,布局正常。
2.4.2 不正常
如上动态添加android:layout_height="fill_parent"就失效,不管这里设置绝对数值如300dp也不行,GridView始终只显示有Item的内容,即容器内的View无法完全填充LinearLayout父容器。
三、 解决代码
就一行代码,不知道是Android的Bug还是怎么:
ly.addView(gridview,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
结束
这个问题烦了我两个小时+,不管怎么说还是解决了,开心ing。