1

我想将此 XML 文件转换为自定义视图,但它不起作用:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/ic_launcher" />

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/imageView1">

        <TextView
            android:id="@+id/posted_content_user"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="posted_content_user"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/posted_content_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/posted_content_user"
            android:layout_alignParentRight="true"
            android:text="59m"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </RelativeLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/relativeLayout1"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/relativeLayout1"
        android:text="TextView 183091480914809 11rh1h23 k1j2h 3oi12u 3o12h3kj12h3iu12 h3kj12h3 12uy3h12kjh31 i2uy3ijh" />

</RelativeLayout>

它应该看起来像这样:

http://i.stack.imgur.com/czXKH.png

这是我生成视图的代码

public void generateView(Context context) {
    this.setLayoutParams(new RelativeLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

    // Contacts Pictures
    ivContactPictures = new ImageView(context);
    ivContactPictures.setLayoutParams(new RelativeLayout.LayoutParams(80,
            80));

    // RelativeLayout
    RelativeLayout relLayoutContent = new RelativeLayout(context);
    RelativeLayout.LayoutParams relLayoutContentParams = new RelativeLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    relLayoutContentParams.addRule(RIGHT_OF, ivContactPictures.getId());
    relLayoutContent.setLayoutParams(relLayoutContentParams);

    // RelativeLayout -> TextView Post User
    posted_content_user = new TextView(context);
    posted_content_user.setLayoutParams(new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    // posted_content_user.setTextAppearance(context,
    // R.attr.textAppearanceMedium);

    // RelativeLayout -> TextView Time
    posted_content_date = new TextView(context);
    RelativeLayout.LayoutParams posted_content_dateParams = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    // posted_content_user.setTextAppearance(context,
    // R.attr.textAppearanceSmall);
    posted_content_dateParams.addRule(ALIGN_BOTTOM,
            posted_content_user.getId());
    posted_content_dateParams.addRule(ALIGN_PARENT_RIGHT);
    posted_content_date.setLayoutParams(posted_content_dateParams);

    // textView Content
    posted_content = new TextView(context);
    RelativeLayout.LayoutParams posted_contentParams = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    posted_contentParams.addRule(ALIGN_LEFT, relLayoutContent.getId());
    posted_contentParams.addRule(ALIGN_PARENT_RIGHT);
    posted_contentParams.addRule(BELOW, relLayoutContent.getId());
    posted_content.setLayoutParams(posted_contentParams);

    // Add RelativeLayout
    this.addView(ivContactPictures);
    relLayoutContent
            .addView(posted_content_user);
    relLayoutContent
            .addView(posted_content_date);
    this.addView(relLayoutContent);
    this.addView(posted_content);

}

结果:http: //i.stack.imgur.com/63e3m.png

我的代码有什么问题吗?

4

1 回答 1

0

这不是您生成自定义视图的方式 - 或者至少是非常困难的方式。相反,您应该做的是创建一个 View 的子级,该子级将该 xml 作为父级膨胀到自身中。这将使该类等于 xml 文件的全部内容。然后,您可以提供任何合适的 getter/setter/其他功能。

于 2013-06-27T08:31:46.453 回答