android中LayoutInflater.from(context).inflate的分析
来源:互联网 发布:淘宝模特拍摄价格 编辑:程序博客网 时间:2024/06/05 06:36
在应用中自定义一个view,需要获取这个view的布局,需要用到
(LinearLayout) LayoutInflater.from(context).inflate(R.layout.contentitem, null);
这个方法。
一般的资料中的第二个参数会是一个null。通常情况下没有问题,但是如果我想给这个view设置一个对应的类,然后通过这个类来操作的话就会出问题。
先看下面的例子
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textViewTitle" android:textColor="@color/black" android:gravity="center" android:textSize="26dp"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textViewAuthor" android:layout_gravity="left" android:textColor="@android:color/darker_gray" android:textSize="16dp"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:layout_gravity="center_horizontal" android:scaleType="center"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textViewContent" android:layout_gravity="center_horizontal" android:textColor="@color/black" android:textSize="20dp"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="2dp" android:layout_gravity="center" android:background="@color/black"> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textViewOtherInfo" android:layout_gravity="left" android:clickable="true" android:textColor="@android:color/darker_gray" android:textSize="16dp"/></LinearLayout>
对应的类是
public class ContentItemView extends LinearLayout { private TextView title; private TextView author; private TextView content; private TextView otherInfo; private ImageView contentImage; private ContentInfo info; public ContentItemView(Context context) { super(context); init(context); } private void init(Context context) { LinearLayout convertView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.contentitem, null); title = (TextView) convertView.findViewById(R.id.textViewTitle); author = (TextView) convertView.findViewById(R.id.textViewAuthor); content = (TextView) convertView.findViewById(R.id.textViewContent); otherInfo = (TextView) convertView.findViewById(R.id.textViewOtherInfo); contentImage = (ImageView) convertView.findViewById(R.id.imageView); }}
这个自定义view我想将它添加到一个listview中。
public void add(final ContentInfo info) { ContentItemView contentItemView = new ContentItemView(context); contentItemView.setContentInfo(info); contentItemView.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT)); data.add(contentItemView); } @Override public View getView(int position, View convertView, ViewGroup parent) { return data.get(position); }
程序运行起来以后,没有任何问题,但是界面没有显示出来,仅仅是在listview中多了一系列黑色的条条.
0 0
- android中LayoutInflater.from(context).inflate的分析
- android中LayoutInflater.from(context).inflate的分析
- android中LayoutInflater.from(context).inflate的分析
- android中LayoutInflater.from(context).inflate的分析
- 关于LayoutInflater.from(context).inflate()的使用的问题
- 关于LayoutInflater.from(context).inflate()的使用的问题
- 关于LayoutInflater.from(context).inflate()的使用的问题
- android关于 LayoutInflater.from(context)中参数的问题
- View.inflate() 和 LayoutInflater.from(context).inflate()区别
- Android中LayoutInflater的使用,inflate
- Android中LayoutInflater的使用,inflate
- 自定义组合控件关于LayoutInflater.from(context).inflate(R.layout.view_title, this,true)的问题
- android中LayoutInflater以及inflate
- android中LayoutInflater以及inflate
- android中LayoutInflater以及inflate
- android中LayoutInflater以及inflate
- android中LayoutInflater以及inflate
- android中LayoutInflater以及inflate
- POJ 3468 线段树 区间增减 区间求和
- java导入https证书
- OC __attribute__
- 什么是PMU(PMIC)
- 设计模式之模板方法模式
- android中LayoutInflater.from(context).inflate的分析
- ListView的优化
- 1058. A+B in Hogwarts (20)
- java程序之源——主函数
- 第十章 Project Setup and Window Initialization
- 推荐一个视频放慢软件(应急用)
- 排序_纪中1386_bzoj4552_树状数组
- Linux-VIM使用
- Android抽象布局——include、merge 、ViewStub