Android:从XML布局创建自定义视图

4
我想创建一个自定义的View(即View类的子类),并使用布局xml资源。
我希望实现以下功能:
public class CustomView extends LinearLayout {

    public CustomView(Context context) {
          super(context);
          LayoutInflater  mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          mInflater.inflate(R.layout.tweet, this, true);
}

这实际上创建了一个具有正确高度和宽度的 View(带有列表的 ScrollView 正好具有预期的长度和滚动条),但它们是空的(黑色的),即使 xml 布局包含许多 TextViews

这是我的 xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tweet"
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"
    android:background="#ffffff">
    <RelativeLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
        <LinearLayout 
            android:layout_height="wrap_content" 
            android:layout_width="match_parent" 
            android:orientation="horizontal"
            android:background="#ffffff">
            <TextView 
                android:text="User" 
                android:id="@+id/tvusername" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:textColor="#000000"
                android:textStyle="bold">
            </TextView>
            <View
                android:layout_width="5dip"
                android:layout_height="1dip">           
            </View>
            <TextView 
                android:text="userscreenname" 
                android:id="@+id/tvuserscreenname" 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </TextView>    
        </LinearLayout>
        <TextView
            android:text="0s"
            android:id="@+id/tvtime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true">
        </TextView>
    </RelativeLayout>
    <TextView 
        android:text="Tweet Content" 
        android:id="@+id/tvcontent" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"
        android:textColor="#000000">
    </TextView>
    <View 
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="#ffffff">
    </View>
    <TextView 
        android:text="Retweet by" 
        android:id="@+id/tvrtby" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </TextView>
</LinearLayout>

这是我的主要布局:

<?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="#ffffff"
    >

<ScrollView 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:id="@+id/scrollView1" 
    android:orientation="vertical"
   android:layout_alignParentTop="true">
    <LinearLayout 
        android:layout_width="match_parent" 
        android:id="@+id/timeline" 
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#ffffff">           
            <TextView  
            android:id="@+id/tvoutput"
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            android:text="@string/hello"
            />    
    </LinearLayout>
</ScrollView>
</LinearLayout>

这是我如何将自定义视图添加到主视图的方式:
未生效(我尝试实现的内容):
TweetView ctweet = new TweetView(getApplicationContext(),tweet);
timeline.addView(ctweet);

我的当前解决方案(可行,但不使用自定义视图):

            View vtweet = View.inflate(getApplicationContext(), R.layout.tweet,null);

            TextView tvusername = (TextView) vtweet.findViewById(R.id.tvusername);
            TextView tvuserscreenname = (TextView) vtweet.findViewById(R.id.tvuserscreenname);
            TextView tvcontent = (TextView) vtweet.findViewById(R.id.tvcontent);
            TextView tvtime = (TextView) vtweet.findViewById(R.id.tvtime);

            tvusername.setText(tweet.getUser().getName());
            tvuserscreenname.setText('@' + tweet.getUser().getScreenName());
            tvcontent.setText(tweet.getText()); 
            //tvtime.setText(ctweet.tvtime.getText()); 
            timeline.addView(vtweet);

发布的 XML 是自定义视图的内容吗? - user
是的,这是资源R.layout.customview的内容。 - Toast
首先,非常感谢您的努力!不幸的是,我无法运行这段代码。它说“Layoutflater 无法解析为类型”,而且我也无法导入它。当我将其更改为 LayoutInflater 时,它可以编译和运行,但结果始终相同。 - Toast
这是指 LayoutInflater 类,我不明白你在我在 GitHub 上发布的代码中到底看到了 Layoutflater(???)。 - user
非常抱歉!我刚刚发现我的浏览器插件会删除每个单词中的序列“In”。我现在已经使您的示例工作,并将尝试在我的主项目中实现相同的效果。我会汇报结果是否有效。再次感谢您的帮助! - Toast
显示剩余9条评论
2个回答

3

您可以像使用任何其他视图一样,在XML中使用此视图,例如 <TextView

尝试使用 <packageName.ClassName ,例如 <com.example.CustomView


我该如何使用它来创建一个类? - Toast

0
可能你需要设置TweetView对象的布局参数。
LinearLayout.LayoutParams fieldparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
ctweet.setLayoutParams(fieldparams);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接