简体   繁体   English

Android,我自己绘制的“分隔”布局无法正常工作

[英]Android, painting on my own, “divided” layout doesn't work

I need to create a layout, which must be divided in half, like this: 我需要创建一个布局,必须将其分成两半,如下所示:

-------
|     |
|  A  |
|     |
-------
|     |
|  B  |
|     |
-------

Where: 哪里:
A - this is the place, where I can draw points with a finger; A-这是我可以用手指画点的地方;
B - it is an example ListView . B-这是一个ListView示例。

It looks like no object from class SampleView was created, so painting in A doesn't work. 看来没有创建类SampleView任何对象,因此在A中绘画不起作用。
What's wrong with that? 怎么了
How do I need to fix painting? 我该如何修复绘画?

drawing_activity.xml : drawing_activity.xml

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



    <View
        class="com.example.proj.SampleView"
        android:id="@+id/sampleView1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight=".50" />



    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".50" >
    </ListView>
</LinearLayout>

SampleView.java : SampleView.java

package com.example.proj;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class SampleView extends View implements OnTouchListener {
    private static final String TAG = "SampleView";
    List<Point> points = new ArrayList<Point>();
    Paint paint = new Paint();

    public SampleView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setFocusable(true);
        setFocusableInTouchMode(true);

        this.setOnTouchListener(this);

        paint.setColor(Color.BLACK);
        paint.setAntiAlias(true);

        Log.i(TAG, "Created SampleView!");
    }

    @Override
    public void onDraw(Canvas canvas) {
        for (Point point : points) {
            canvas.drawCircle(point.x, point.y, 5, paint);
            Log.d(TAG, "Painting: "+point);
        }
    }

    public boolean onTouch(View view, MotionEvent event) {
        if(event.getAction() != MotionEvent.ACTION_DOWN)
           return super.onTouchEvent(event);
        Point point = new Point();
        point.x = event.getX();
        point.y = event.getY();
        points.add(point);
        invalidate();
        Log.d(TAG, "point: " + point);
        return true;
    }

}


class Point {
    float x, y;

    @Override
    public String toString() {
        return x + ", " + y;
    }
}

MainActivity.java : MainActivity.java

package com.example.proj;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.widget.ListView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drawing_activity);
        final ListView listview = (ListView) findViewById(R.id.listView1);
        String[] values = new String[] { "Element1", "Element2" };

        final ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < values.length; ++i) {
          list.add(values[i]);
        }
        final StableArrayAdapter adapter = new StableArrayAdapter(this,
            android.R.layout.simple_list_item_1, list);
        listview.setAdapter(adapter);
    }

}

In your xml must be: 在您的xml中必须是:

<com.example.proj.SampleView
        android:id="@+id/sampleView1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight=".50" />

OR: view with non -capitalized "V" 或:使用大写的“ V”视图

<view
        class="com.example.proj.SampleView"
        android:id="@+id/sampleView1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight=".50" />

Both tags view and View exist, however View will generate a new View class whereas view will generate from the class tag. 标签view和View都存在,但是View将生成一个新的View类,而view将通过class标签生成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM