gpt4 book ai didi

android - Android 中只显示一个 View

转载 作者:行者123 更新时间:2023-11-29 22:24:22 25 4
gpt4 key购买 nike

我是 Android 新手。当我运行这段代码时,只显示一个圆圈。如果我删除 view1,则会显示 view2。但他们从来没有一起展示过!!!这是为什么?任何帮助将不胜感激。

谢谢

package com.dots;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class Dots1Activity extends Activity
{
private static final String TAG = "DotsActivity";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
TextView label = new TextView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
label.setText("Click the circle!");
CustomDrawableView view1 = new CustomDrawableView(this, 100, 100, 50, Color.RED);
CustomDrawableView view2 = new CustomDrawableView(this, 200, 200, 25, Color.GREEN);
CustomDrawableView view3 = new CustomDrawableView(this, 300, 300, 10, Color.WHITE);
ll.addView(label, layoutParams);
ll.addView(view1, layoutParams);
ll.addView(view2, layoutParams);
ll.addView(view3, layoutParams);
setContentView(ll);
}

}

class CustomDrawableView extends View implements OnClickListener{
private Context context;
private int x, y, radius, color;
public CustomDrawableView(Context context, int x, int y, int radius, int color) {
super(context);
this.context = context;
this.x = x;
this.y =y;
this.radius = radius;
this.color = color;
setOnClickListener(this);
}

protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
this.setBackgroundColor(Color.LTGRAY);
Paint paint = new Paint (Paint.ANTI_ALIAS_FLAG);
paint.setColor(color);
canvas.drawCircle(x, y, radius, paint);

}

public void onClick(View v) {
Toast.makeText(this.context,
x+"-"+y+"-"+radius,
Toast.LENGTH_SHORT).show();
}
}

最佳答案

ll.setOrientation(LinearLayout.HORIZONTAL);

它们并排放置。你看不到它们,因为你的显示器不是宽度为零。将它们放在 HorizontalScrollView 中或者使它们成为 VERTICAL


我不确定这是否在这里生效,但我在 Android Documentation 上找到了这个:

Note that the framework will not draw views that are not in the invalid region. To force a view to draw, call invalidate().

试试这是否能解决您的问题(我猜是暂时的)。


关于您的代码

我注意到:onClickListener 的实现接口(interface)是 View.OnClickListener:

class CustomDrawableView extends View implements View.OnClickListener{ [...] }

如何解决问题

我查看了 Android 文档并找到了 this .他们提到了方法 onMeasure() ,其中:

Measure the view and its content to determine the measured width and the measured height.

所以我将它添加到您的自定义 CustomDrawableView 类中。不幸的是,您不能传递 super.onMeasure() 方法简单的整数,您需要先使用 View.MeasureSpec 对它们进行解码。 -类:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY));
}

在示例中,我将宽度和高度都设置为 100px。另外,我对您的代码做了一些其他改进:

Dots1Activity 类

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Dots1Activity extends Activity
{
private static final String TAG = "DotsActivity";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TextView label = new TextView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
label.setText("Click the circle!");
CustomDrawableView view1 = new CustomDrawableView(this, 50, 50, 50, Color.RED);
CustomDrawableView view2 = new CustomDrawableView(this, 75, 75, 25, Color.GREEN);
CustomDrawableView view3 = new CustomDrawableView(this, 85, 85, 10, Color.WHITE);
ll.addView(label, layoutParams);
ll.addView(view1, layoutParams);
ll.addView(view2, layoutParams);
ll.addView(view3, layoutParams);
setContentView(ll);
}

}

CustomDrawableView 类

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;
import android.widget.Toast;

class CustomDrawableView extends View implements View.OnClickListener{
private Context context;
private int x, y, radius, color;
public CustomDrawableView(Context context, int x, int y, int radius, int color) {
super(context);
this.context = context;
this.x = x;
this.y =y;
this.radius = radius;
this.color = color;
setOnClickListener(this);
}

protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(color);
canvas.drawCircle(x, y, radius, paint);
}

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
super.onMeasure(View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(100, View.MeasureSpec.EXACTLY));
}

public void onClick(View v) {
Toast.makeText(this.context,
x+"-"+y+"-"+radius,
Toast.LENGTH_SHORT).show();
}
}

此代码可以编译,显示所有圆圈,并且 onClick-Event 也有效。

虽然我不得不说这是一个挑战,但我很感激。

关于android - Android 中只显示一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6336741/

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