【问题标题】:How to add custom view to the layout?如何在布局中添加自定义视图?
【发布时间】:2017-07-14 19:58:03
【问题描述】:

我有一个从View 类扩展而来的GraphicsView 类,我想将此GraphicsView 类添加到我项目的主布局中。我该怎么做?

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

还有我的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>

【问题讨论】:

  • 我必须把这个放在哪里?

标签: android android-layout android-view


【解决方案1】:

您需要提供扩展 View 的类的完整路径,

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

【讨论】:

  • 它给了我例外! GraphicsView 类在活动类中
  • 为 GraphicsView 类创建一个单独的类。
  • 它仍然给我例外这里是代码
  • 我把这段代码放在 main.xml 中对吗?我的包名是包 gen.customfonts.namespace;那么,使用我之前发送给您的视图的权利是吗?感谢您的帮助
【解决方案2】:

如果我没记错的话,你需要提供更多的构造函数来使用 xml 文件中的视图(将其添加到 xml 文件中,如“我和我们”建议的那样)。

public GraphicsView(Context context) {
    super(context);
}

public GraphicsView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public GraphicsView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

更新:固定代码。

【讨论】:

  • 谢谢,这很有帮助。
  • 我在使用单个构造函数时遇到了错误,但是在使用了所有这三个之后,我没有遇到错误!
【解决方案3】:

我终于明白了,这是代码 XML 代码

<com.customfonts.namespace.BreakDownBar
         android:id="@+id/gview"
          android:layout_width="fill_parent"
        android:layout_height="20dip"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"
        android:background="@color/BreakDownBarBack"/>

和班级

package com.customfonts.namespace;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.util.AttributeSet;
import android.view.View;



public class BreakDownBar extends View {

    public BreakDownBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

        @Override
        protected void onDraw(Canvas canvas) {
                //Draw rectangle;
                Path rect = new  Path();
                rect.addRect(0, 0,250, 150,Direction.CW);
                Paint cpaint = new Paint();
                cpaint.setColor(Color.GREEN); 
                canvas.drawPath(rect, cpaint);
        }
}

【讨论】:

    【解决方案4】:

    你需要这样做:

    LinearLayout v = (LinearView) findViewById(R.id.linnnnlayout);
    GraphicsView myView = new myGraphicsView(this);
    v.addView(myView);
    

    【讨论】:

      【解决方案5】:

      因为您的自定义ViewActivity 中的一个内部类,java 编译器将为该类输出名称ActivityName$GraphicsView。由于$ 字符,您不能将该名称直接用作xml 布局中的View 名称,但您可以这样做:

       <view 
          class="com.package.here.ActivityName$GraphicsView"
          android:id="@+id/view"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"/>
      

      其中ActivityName 是声明GraphicsView 类的活动的名称。

      【讨论】:

      • 我已将课程放在单独的文件中。但我有异常说错误膨胀类 com.gen.customfonts.namespace.GraphicsView 我认为我的类名有问题!包名是包 gen.customfonts.namespace;我应该在 xml 中调用什么类?
      • @AnasBakez 您是否尝试过将您之前拥有的GraphicsView 课程留在活动中,并使用我回答中的代码?您还应该实现 View 超类的其他 2 个构造函数。
      • 我试过了,但它什么也没做! GraphicsView 类中的视图不可见,但它没有给出异常,我认为 iam 放置的类名是错误的。包名是包 gen.customfonts.namespace;活动名称是 CustomFontsActivity 类名应该是什么?
      • @AnasBakez 试试:class="gen.customfonts.namespace.CustomFontsActivity$GraphicsView"
      • @AnasBakez 我想我找到了问题所在。将上面的代码与class 属性一起使用,而不是&lt;View class="... 使用&lt;view class="...view 没有大写字母)。另外不要忘记实现View 超类中的其他两个构造函数。
      【解决方案6】:
      public class MainActivity extends Activity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
      
      
          LinearLayout v = (LinearLayout) findViewById(R.id.linearLayout);
          MyGraphics myView = new MyGraphics(this);
          v.addView(myView);
      }
      }
      
      
      public class MyGraphics extends View {
      public MyGraphics(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);
      }
      
      
      }
      

      XML:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" 
      android:id="@+id/linearLayout">
      

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

      【讨论】:

        【解决方案7】:

        这对我有用,并使用完整路径在 XML 中添加视图,并尝试为高度和宽度提供 wrapcontent。

        public class RectangleView extends View {
            public RectangleView(Context context) {
                super(context);
            }
        
            public RectangleView(Context context, AttributeSet attrs) {
                super(context, attrs);
            }
        
            public RectangleView(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
            }
        
            @Override
            protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);
                Paint paint = new Paint();
                paint.setColor(Color.GRAY);
                canvas.drawColor(Color.BLUE);
                canvas.drawRect(10,10,50,50, paint);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-29
          • 1970-01-01
          相关资源
          最近更新 更多