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