栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Android中以编程方式创建具有自定义列表项的ListView-无xml列表项布局

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Android中以编程方式创建具有自定义列表项的ListView-无xml列表项布局

ListView
扩展
AbsListView
,这又
AdapterView
扩展了视图组。因此,的所有子项
ListView
都将添加到中
ViewGroup
。因此,要设置布局参数,可以在为设置布局参数时使用ViewGroup.LayoutParam
listLayout

或者尝试

AbsListView.LayoutParams
设置布局参数
listLayout

请尝试一下,让我知道它是否有效。

以下代码工作正常。

public class MainActivity1 extends Activity {    String[] wordlist = new String[] { "a", "b", "c" };    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        ListView list = new ListView(this);        list.setAdapter(new MyAdapter(this, wordlist));        setContentView(list);    }    private class MyAdapter extends ArrayAdapter<String> {        public MyAdapter(Context context, String[] strings) { super(context, -1, -1, strings);        }        @Override        public View getView(int position, View convertView, ViewGroup parent) { LinearLayout listLayout = new LinearLayout(MainActivity1.this); listLayout.setLayoutParams(new AbsListView.LayoutParams(         AbsListView.LayoutParams.WRAP_CONTENT,         AbsListView.LayoutParams.WRAP_CONTENT)); listLayout.setId(5000); TextView listText = new TextView(MainActivity1.this); listText.setId(5001); listLayout.addView(listText);        listText.setText(super.getItem(position)); return listLayout;        }    }}

问题在于,默认情况下,与Eclipse导入

ViewGroup.LayoutParams
不兼容的Eclipse导入
AbsListView.LayoutParams
需要用于设置从
getView()
方法返回的视图的布局参数。

请检查,让我知道如何进行。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/508984.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号