如何添加自定义视图的布局? [英] How to add custom view to the layout?

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

问题描述

我有一个 GraphicsView 类从查看类扩展,我想添加此 GraphicsView 类在我的项目主要布局。我该怎么办呢?

 的静态公共类GraphicsView扩展视图{
        公共GraphicsView(上下文的背景下){
        超(上下文);
        }
        @覆盖
        保护无效的OnDraw(帆布油画){
        //绘图命令去这里
            路径RECT =新路径();
            rect.addRect(100,100,250,50,Direction.CW);
            油漆CPAINT =新的油漆();
            cPaint.setColor(Color.LTGRAY);
            canvas.drawPath(矩形,CPAINT);
        }
    }
 

和我的的main.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / linnnnlayout
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>

    <的TextView
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=@字符串/你好/>

    <的TextView
        机器人:ID =@ + ID / CustomFont的
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=@字符串/你好/>

     <查看
         机器人:ID =@ + ID /视图
          机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT/>

< / LinearLayout中>
 

解决方案

您需要给你的类扩展查看完整的路径,

 < com.blah.blah.GraphicsView
         机器人:ID =@ + ID /视图
          机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT/>
 

I have a GraphicsView class that extends from the View class and I want to add this GraphicsView class to the main layout in my project. How can I do that?

static public class GraphicsView extends View {
        public GraphicsView(Context context) {
        super(context);
        }
        @Override
        protected void onDraw(Canvas canvas) {
        // Drawing commands go here
            Path rect = new  Path();
            rect.addRect(100, 100, 250, 50, Direction.CW);
            Paint cPaint = new Paint();
            cPaint.setColor(Color.LTGRAY); 
            canvas.drawPath(rect, cPaint);
        }
    }

and my main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linnnnlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <TextView 
        android:id="@+id/Customfont"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

     <View 
         android:id="@+id/view"
          android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

解决方案

You need to give complete path of your class that extends View,

<com.blah.blah.GraphicsView
         android:id="@+id/view"
          android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

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

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