【问题标题】:Android: Custom view from xml layoutAndroid:来自 xml 布局的自定义视图
【发布时间】:2012-07-16 10:44:11
【问题描述】:

我想创建一个自定义 ViewView 类的子类)并使用布局 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是自定义View的内容吗?
  • 没错,就是资源R.layout.customview的内容
  • 我猜你在你发布的代码中夸大了布局? xml布局中使用自定义视图还是实例化?
  • 我想通过调用构造函数来实例化它,然后将它作为子项添加到我的主要活动中的 LinearLayout 中。
  • 我看不出可能出了什么问题,因此您应该添加更多详细信息,尤其是添加自定义视图的布局以及用于添加自定义视图的代码。

标签: android xml layout view


【解决方案1】:

你可以像&lt;TextView这样的任何其他视图一样在你的xml中使用它

试试&lt;packageName.ClassName,比如&lt;com.example.CustomView

【讨论】:

  • 我怎样才能用它来制作一个类呢?
【解决方案2】:

可能您必须设置 TweetView 对象的布局参数。

LinearLayout.LayoutParams fieldparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
ctweet.setLayoutParams(fieldparams);

【讨论】:

    猜你喜欢
    • 2013-07-24
    • 2011-07-28
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多