Android 如何在布局中的特定位置添加视图(在java类中描述)?

Android 如何在布局中的特定位置添加视图(在java类中描述)?,android,android-linearlayout,android-canvas,surfaceview,Android,Android Linearlayout,Android Canvas,Surfaceview,这是我的xml文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout0" android:layout_width="match_parent" andr

这是我的xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout0"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CanvasActivity"
android:baselineAligned="false">

<LinearLayout
    android:id="@+id/LinearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" 
    android:layout_weight="1"
    android:background="#f9dfcb">  
</LinearLayout>

<LinearLayout
    android:id="@+id/LinearLayout2"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="3"
    android:background="#000000" 
    >


</LinearLayout>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".CanvasActivity"
android:baselineAligned="false">


 <View
    android:id="@+id/view0"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" 
    android:layout_weight="3"
    android:background="#f9dfcb"

/>
<com.multitel.testwidget.Canvasview
                xmlns:android="http://schemas.android.com/apk/res/android" 
                android:id="@+id/surfaceView1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">                  
</com.multitel.testwidget.Canvasview>


</LinearLayout>
我的画布是这样的

public class Canvasview extends SurfaceView implements SurfaceHolder.Callback{

private int width, height;
private Paint textPaint = new Paint();
private Paint touchPaint = new Paint();
private int colors[] = new int[10];

public Canvasview(Context context) {
    super(context);
    SurfaceHolder holder = getHolder();
    holder.addCallback(this);

    setFocusable(true); // make sure we get key events
    setFocusableInTouchMode(true); // make sure we get touch events
    init();
}

private void init() 
{
    textPaint.setColor(Color.WHITE);
    colors[0] = Color.RED;


    touchPaint = new Paint();
    touchPaint.setColor(colors[0]);

}

public boolean onTouchEvent(MotionEvent event) 
{
    Canvas c = getHolder().lockCanvas();

    if (c != null) 
    {

        if (event.getAction() == MotionEvent.ACTION_UP) 
        {
            // clear everything

        }
        else 
        {
            int xval = (int)event.getX();
            int yval = (int)event.getY();
            drawCircle(xval,yval,touchPaint,c);
        }

        getHolder().unlockCanvasAndPost(c);
    }



    return true;

}


public void clear(Canvas c){
    c = getHolder().lockCanvas();
    c.drawColor(Color.BLACK);
    getHolder().unlockCanvasAndPost(c);

}


private void drawCircle(int x, int y, Paint paint, Canvas c) 
{
    c.drawCircle(x, y, 2, paint);

}



@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){

    Canvas c = getHolder().lockCanvas();




    if (c != null) 
    {
        // clear screen
        c.drawColor(Color.BLACK);

        getHolder().unlockCanvasAndPost(c);
    }
}

@Override
public void surfaceCreated(SurfaceHolder arg0) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
    // TODO Auto-generated method stub

}

}
我只需要linearlayout2中的画布,但画布占据整个屏幕。我不确定我会错在哪里

编辑:
我做了一些更改,在linearlayout2中得到了Canvasview,Canvasview的ontouchevents检测到触摸事件,但在Canvasview public void surfaceChanged(SurfaceHolder holder,int format,int width,int height)中,方法不会初始化画布,因此它总是空的。要绘制,我需要画布不为空。非常感谢您的帮助

为第一个线性布局设置布局宽度的硬编码值,然后重试

第一个问题是,您需要获取尚未在onCreate()中绘制的视图的高度和宽度:

一种可能的解决方案是设置静态值或使用ViewTreeObserver()

您可能遇到的另一个问题是,线性布局不调用onTouch()方法。您可以尝试使用滚动视图(例如滚动视图)而不是线性布局。

我更改了xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout0"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CanvasActivity"
android:baselineAligned="false">

<LinearLayout
    android:id="@+id/LinearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" 
    android:layout_weight="1"
    android:background="#f9dfcb">  
</LinearLayout>

<LinearLayout
    android:id="@+id/LinearLayout2"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="3"
    android:background="#000000" 
    >


</LinearLayout>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".CanvasActivity"
android:baselineAligned="false">


 <View
    android:id="@+id/view0"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" 
    android:layout_weight="3"
    android:background="#f9dfcb"

/>
<com.multitel.testwidget.Canvasview
                xmlns:android="http://schemas.android.com/apk/res/android" 
                android:id="@+id/surfaceView1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">                  
</com.multitel.testwidget.Canvasview>


</LinearLayout>

我可以在画布上画画

尝试给主
LiniarLayout
a
android:weightSum=“4”
。但我建议你找到另一种方法。。。权重参数不是命令的,因为布局计算了两次。我会使用Andoid PopupWindow,如果它是您希望浮动在主活动屏幕上的内容,或者时间倒转,请使用Andoid 2-3 API,只需使用绝对布局即可。谢谢您的帮助。添加了一个editWell我更改了代码的某些部分LinearLayout LinearLayout2=(LinearLayout)findViewById(R.id.LinearLayout2);int width=LinearLayout2.getWidth();int height=LinearLayout2.getHeight();线性布局2.添加视图(新画布视图(本)、宽度、高度);但现在画布并没有检测到OnTouchVenti,只是编辑了部分代码,添加了触摸方法。问题是surfaceChanged(SurfaceHolder holder,int format,int width,int height)需要初始化画布,而它没有进入代码的该部分。所以,即使我可以在触摸屏上检测到事件,我的画布是空的,因此我不能在上面画画,我会尝试你的建议
public Canvasview(Context context, AttributeSet attrs) 
{
    super(context, attrs);
    // TODO Auto-generated constructor stub
    SurfaceHolder holder = getHolder();
    holder.addCallback(this);
    setFocusable(true); // make sure we get key events
    setFocusableInTouchMode(true); // make sure we get touch events
    init();
    holder.getSurface();
}