Android 如何将自定义视图添加到布局?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10410616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:36:44  来源:igfitidea点击:

How to add custom view to the layout?

androidandroid-layoutandroid-view

提问by AnasBakez

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

我有一个GraphicsView从类扩展的View类,我想将这个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);
        }
    }

and my main.xml:

和我的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>

回答by Lalit Poptani

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"/>

回答by Sver

If I remember correctly, you need to provide more constructors to use view from xml file (add it to xml file like "Me and We" suggested).

如果我没记错的话,您需要提供更多构造函数来使用 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);
}

Update: fixed code.

更新:固定代码。

回答by AnasBakez

i finaly got it here is the code the XML code

我最终得到了它是代码 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"/>

and the class

和班级

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

回答by thepoosh

you need to do this:

你需要这样做:

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

回答by Luksprog

Because your custom Viewis an inner class in your Activitythe java compiler will output the name ActivityName$GraphicsViewfor that class. You can't use that name directly as the Viewname in the xml layout because of the $character but you can do it like this:

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

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

where ActivityNameis the name of the activity where your GraphicsViewclass is declared.

where是声明ActivityName您的GraphicsView类的活动的名称。

回答by M.Praveen Kumar

This is working for me and add the view in XML with full path and try giving wrapcontent for height and width.

这对我有用,并在带有完整路径的 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);
    }
}

回答by Krishnakant Dalal

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:

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>