博客
关于我
[android] 采用layoutInflater打气筒创建一个view对象
阅读量:655 次
发布时间:2019-03-15

本文共 1328 字,大约阅读时间需要 4 分钟。

上一节知道了ListView的工作原理,数据也展示出来了,但是TextView显示的非常难看,如果想美化一下,就先创建好一个布局出来,这个布局采用了两层LinearLayout嵌套,外层的水平方向,内层的竖直方向,android:gravity=”center_vertical”内容竖直居中,android:layout_marginLeft左边距,android:textColor=”#55000000”前两个是透明度等。

 

在上一节的adapter实现类中,getView()方法中,想办法把xml文件转成可以显示的View对象,调用View类的inflate(context,resuorce,root)方法,context上下文,内部类调用外部类Activity的对象MainActivity.thisresource资源例如R.layout.listroot是想把这个View对象填充进去的父容器,我们这里只需返回这个对象,不用填充到父容器,写上null,这时候拿到了转换后的View对象

 

修改拿到的View对象里面控件的展示信息,调用View对象的findViewById()方法,找到对应的控件,调用TextView对象的setText()设置信息,在调用setText()方法的时候,如果传入的是个int类型会报错ResuorceNotFound,因为这个api可以传入String资源id,资源idint的,因此会误以为资源id,报找不到资源,所以setText()int的时候一定要转一下

 

activity:

 

@Override        public View getView(int position, View convertView, ViewGroup parent) {            Person person=persons.get(position);                        View list=View.inflate(MainActivity.this, R.layout.list_item, null);            TextView tv_id=(TextView) list.findViewById(R.id.tv_id);            TextView tv_name=(TextView) list.findViewById(R.id.tv_name);            TextView tv_account=(TextView) list.findViewById(R.id.tv_account);                        tv_id.setText("id:"+person.getId());            tv_name.setText("姓名:"+person.getName());            tv_account.setText("金额"+person.getId());                        return list;        }

 

布局:

 

转载地址:http://ggjmz.baihongyu.com/

你可能感兴趣的文章
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No static resource favicon.ico.
查看>>
no such file or directory AndroidManifest.xml
查看>>