Android中只显示一个视图

时间:2011-06-13 21:41:57

标签: android view android-layout

我是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();  
    }
}

2 个答案:

答案 0 :(得分:1)

ll.setOrientation(LinearLayout.HORIZONTAL);

它们彼此相邻。你看不到它们,因为你的显示器没有宽度。将它们放在HorizontalScrollView中,或者将它们变成VERTICAL


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

  

请注意,框架不会绘制   视图不在无效中   区域。要强制绘制视图,请调用   无效()。

试试这是否解决了你的问题(我猜的那一刻)。


关于您的代码

我注意到的一点:onClickListener的实施界面是View.OnClickListener

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

如何解决问题

我浏览了Android文档并找到了this。他们提到了方法onMeasure(),其中:

  

测量视图及其内容   确定测量的宽度和   测量高度。

所以我将它添加到您的自定义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也可以。

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

答案 1 :(得分:1)

将布局的方向更改为垂直,如下所示:

ll.setOrientation(LinearLayout.VERTICAL);