gpt4 book ai didi

java - Android,自己画的, "divided"布局不行

转载 作者:行者123 更新时间:2023-11-29 21:19:26 27 4
gpt4 key购买 nike

我需要创建一个布局,它必须分成两半,如下所示:

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

地点:
A - 这是我可以用手指画点的地方;
B - 这是一个示例 ListView

看起来类 SampleView 中的对象没有被创建,所以在 A 中绘制不起作用。
这有什么问题吗?
我需要如何修复绘画?

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:

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:

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

}

最佳答案

在你的 xml 中必须是:

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

或者:用大写的“V”查看

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

view 和 View 标签都存在,但是 View 将生成一个新的 View 类,而 view 将从 class 标签生成。

关于java - Android,自己画的, "divided"布局不行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20938114/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com