首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将安卓自定义VIew转换为代码

将安卓自定义VIew转换为代码
EN

Stack Overflow用户
提问于 2013-06-27 16:28:02
回答 1查看 534关注 0票数 1

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

代码语言:javascript
复制
<?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

这是我生成的视图的代码

代码语言:javascript
复制
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

我的代码有什么问题吗?

EN

回答 1

Stack Overflow用户

发布于 2013-06-27 16:31:46

这不是生成自定义视图的方式--或者至少很难实现。您应该做的是创建一个子视图子视图,它将xml扩展到自身作为父视图。这将使该类等同于xml文件的全部内容。然后,您可以提供任何合适的getter/setter/其他功能。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17338286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档