安卓:从XML布局自定义视图 [英] Android: Custom view from xml layout

查看:168
本文介绍了安卓:从XML布局自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义的查看(即查看类的子类),并使用一个布局XML资源

我想是这样的:

 公共类CustomView扩展的LinearLayout {    公共CustomView(上下文的背景下){
          超级(上下文);
          LayoutInflater mInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          mInflater.inflate(R.layout.tweet,这一点,真正的);
}

这实际上创建一个查看与正确的高度和宽度(滚动型与人的名单有准确预期的长度和滚动条),但它们是空的(黑色),即使XML布局中含有大量的 TextViews

这是我的XML布局:

 <?XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID /鸣叫
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直
    机器人:背景=#FFFFFF>
    < RelativeLayout的
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_width =match_parent>
        <的LinearLayout
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_width =match_parent
            机器人:方向=横向
            机器人:背景=#FFFFFF>
            <的TextView
                机器人:文字=用户
                机器人:ID =@ + ID / tvusername
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文字颜色=#000000
                机器人:文字样式=大胆>
            < / TextView的>
            <查看
                机器人:layout_width =5dip
                机器人:layout_height =1dip>
            < /视图>
            <的TextView
                机器人:文字=userscreenname
                机器人:ID =@ + ID / tvuserscreenname
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT>
            < / TextView的>
        < / LinearLayout中>
        <的TextView
            机器人:文字=0
            机器人:ID =@ + ID / tvtime
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_alignParentRight =真正的>
        < / TextView的>
    < / RelativeLayout的>
    <的TextView
        机器人:文字=鸣叫内容
        机器人:ID =@ + ID / tvcontent
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:文字颜色=#000000>
    < / TextView的>
    <查看
        机器人:layout_width =match_parent
        机器人:layout_height =1像素
        机器人:背景=#FFFFFF>
    < /视图>
    <的TextView
        机器人:文字=被转推
        机器人:ID =@ + ID / tvrtby
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT>
    < / TextView的>
< / LinearLayout中>

这是我的主要布局:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:背景=#FFFFFF
    ><滚动型
    机器人:layout_height =match_parent
    机器人:layout_width =match_parent
    机器人:ID =@ + ID / scrollView1
    机器人:方向=垂直
   机器人:layout_alignParentTop =真正的>
    <的LinearLayout
        机器人:layout_width =match_parent
        机器人:ID =@ + ID /时间线
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=垂直
        机器人:背景=#FFFFFF>
            <的TextView
            机器人:ID =@ + ID / tvoutput
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:文字=@字符串/你好
            />
    < / LinearLayout中>
< /滚动型>
< / LinearLayout中>

这是我如何自定义视图添加到我的主要观点:

不工作(我尽量做到):

  TweetView ctweet =新TweetView(getApplicationContext(),鸣叫);
timeline.addView(ctweet);

我目前的解决方案(的作品,但不使用自定义视图):

 查看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使用,就像像任何其他视图<的TextView

尝试< packageName.ClassName 即像< com.example.CustomView

i would like to create a custom View (subclass of the View class) and use a layout xml resource.

I want something like this:

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);
}

This actually creates a View with the right height and width (a ScrollView with a list of those has exactly the expected length and scrollbars) but they are empty (black) even though the xml layout contains a lot of TextViews.

This is my xml layout:

<?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>

This is my main layout:

<?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>

This is how I add the custom view to my main view:

Not working (what I try to achieve):

TweetView ctweet = new TweetView(getApplicationContext(),tweet);
timeline.addView(ctweet);

My current solution (works, but uses no custom view):

            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);

解决方案

you can use this in your xml just like any other view like <TextView

try <packageName.ClassName ie something like <com.example.CustomView

这篇关于安卓:从XML布局自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆