没有合适的资源?快使用搜索试试~ 我知道了~
后台代码 代码如下: 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);
资源详情
资源评论
资源推荐
Android动态添加动态添加View的问题解决方法的问题解决方法
后台代码
代码如下:
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);
}
代码说明:
a). ly为main.xml中id为ContentView的LinearLayout,即需动态添加View的容器
b). ItemAdapter为Grid填充数据的辅助类
现象
正常
如果把grid.xml中GridView的代码直接复制粘贴到main.xml中LinearLayout容器内没有任何问题,布局正常。
不正常
如上动态添加android:layout_height=”fill_parent”就失效,不管这里设置绝对数值如300dp也不行,GridView始终
只显示有Item的内容,即容器内的View无法完全填充LinearLayout父容器。
三、 解决代码
就一行代码,不知道是Android的Bug还是怎么:
代码如下:
ly.addView(gridview,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
weixin_38665822
- 粉丝: 9
- 资源: 933
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0