当前位置 : 主页 > 网页制作 > xml >

如何在没有xml的条形图中添加按钮?

来源:互联网 收集:自由互联 发布时间:2021-06-13
我画了一个没有xml的条形图.我必须在图表下面添加一个按钮.任何人都可以建议解决方案.谢谢你提前!!以下是我的程序. GraphViewDemo.java public class GraphViewDemo extends Activity {public static Strin
我画了一个没有xml的条形图.我必须在图表下面添加一个按钮.任何人都可以建议解决方案.谢谢你提前!!以下是我的程序.

GraphViewDemo.java

public class GraphViewDemo  extends Activity {

public static String graphreturn;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    float[] values = new float[] { 2.0f,3.0f, 5.0f, 1.0f , 6.0f , 4.0f ,7.0f };
    String[] verlabels = new String[] { "7","6","5","4","3", "2", "1" };
    String[] horlabels = new String[] {"jan20","jan21","jan22","jan23","jan24",
                               "jan25","jan26"};
  GraphView graphView = new GraphView(this, values, "GraphViewDemo",horlabels,    verlabels, GraphView.BAR);
  setContentView(graphView);}}

GraphView.java

public class GraphView extends View{

public static boolean BAR = true;
public static boolean LINE = false;

private Paint paint;
private float[] values;
private String[] horlabels;
private String[] verlabels;
private String title;
private boolean type;
Context context;
private Drawable mDrawable;
private Runnable in ;


public GraphView(Context context, float[] values, String title, String[] horlabels,String[] verlabels, boolean type) {

    super(context);
    if (values == null)
        values = new float[0];
    else
        this.values = values;
    if (title == null)
        title = "";
    else
        this.title = title;
    if (horlabels == null)
        this.horlabels = new String[0];
    else
        this.horlabels = horlabels;
    if (verlabels == null)
        this.verlabels = new String[0];
    else
        this.verlabels = verlabels;
    this.type = type;
    paint = new Paint();
}

@Override
protected void onDraw(final Canvas canvas) {
    context=getContext();
    float border = 20;
    float horstart = border * 2;
    float height = getHeight()-50;
    float width = getWidth();
    float max = getMax();
    Log.w("max", ""+max);
    float min = getMin();
    Log.w("min", ""+min);
    float diff = max - min;
    float graphheight = height - (2 * border);
    float graphwidth = width - (2 * border);

    paint.setTextAlign(Align.LEFT);
    int vers = verlabels.length - 1;
    for (int i = 0; i < verlabels.length; i++) {
        paint.setColor(Color.DKGRAY);
        float y = ((graphheight / vers) * i) + border;
        canvas.drawLine(horstart, y, width, y, paint);
        paint.setColor(Color.WHITE);
        canvas.drawText(verlabels[i], 0, y, paint);
    }
    int hors = horlabels.length;
    for (int i = 0; i < horlabels.length; i++) {
        paint.setColor(Color.DKGRAY);
        float x = ((graphwidth / hors) * i) + horstart;
        canvas.drawLine(x, height - border, x, border, paint);
        paint.setTextAlign(Align.CENTER);
        if (i==horlabels.length+5)
            paint.setTextAlign(Align.RIGHT);
        if (i==0)
            paint.setTextAlign(Align.LEFT);
        paint.setColor(Color.WHITE);
        canvas.drawText(horlabels[i], x, height - 4, paint);
    }

    paint.setTextAlign(Align.CENTER);
    canvas.drawText(title, (graphwidth / 2) + horstart, border - 4, paint);


    int x = 0;
    int y = 0;
    Paint paint = new Paint();
    Paint paint1 = new Paint();
    paint.setStyle(Paint.Style.FILL);
    String str2rotate = "Rotated!";
    // draw bounding rect before rotating text
    Rect rect = new Rect();
    paint.getTextBounds(str2rotate, 0, str2rotate.length(), rect);
    canvas.translate(x, y);
    paint.setColor(Color.GREEN);
    paint.setStyle(Paint.Style.FILL);
    paint.setStrokeWidth(1);

    paint.setStyle(Paint.Style.STROKE);

    paint1.setColor(Color.RED);
    canvas.drawText("!Rotated", 0, 0, paint1);


    mDrawable = context.getResources().getDrawable(R.drawable.previousi);
    mDrawable.setBounds(getWidth()/2-40,getHeight()-40, getWidth()/2+30, getHeight()-20);
    mDrawable.draw(canvas);

    mDrawable.scheduleSelf(in, 0);

    //....................
    in = new Runnable() {
        public void run() {

                try {


                    //canvas.restore();
                     mDrawable.setBounds(getWidth()/2,getHeight(), getWidth(), getHeight()-20);
                        mDrawable.draw(canvas);




                } 
                 catch (Exception e) { 
                        e.printStackTrace();
                    }



            } 

    };

    Thread thread = new Thread(null, in, "graphview");
    thread.start();






    if (max != min) {
        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.FILL);

        if (type == BAR) {
            float datalength = values.length;
            float colwidth = (width - (2 * border)) / datalength;
            for (int i = 0; i < values.length; i++) {
                float val = values[i] - min;
                float rat = val / diff;
                float h = graphheight * rat;
                canvas.drawRect((i * colwidth) + horstart, (border - h) + graphheight, ((i * colwidth) + horstart) + (colwidth - 1), height - (border - 1), paint);                     

            }
        } else {
            float datalength = values.length;
            float colwidth = (width - (2 * border)) / datalength;
            float halfcol = colwidth / 2;
            float lasth = 0;
            for (int i = 0; i < values.length; i++) {
                float val = values[i] - min;
                float rat = val / diff;
                float h = graphheight * rat;
                if (i > 0)
                    canvas.drawLine(((i - 1) * colwidth) + (horstart + 1) + halfcol, (border - lasth) + graphheight, (i * colwidth) + (horstart + 1) + halfcol, (border - h) + graphheight, paint);
                lasth = h;
            }
        }
    }
}



private float getMax() {
    float largest = Integer.MIN_VALUE;
    for (int i = 0; i < values.length; i++)
        if (values[i] > largest)
            largest = values[i];
    return largest;
}

private float getMin() {
    float smallest = Integer.MAX_VALUE;
    for (int i = 0; i < values.length; i++)
        if (values[i] < smallest)
            smallest = values[i];
    return smallest;
}


}
试试这个:

btn = new Button(this);
 btn.setText("Hello Button");
 RelativeLayout.LayoutParams paramsd = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONT ENT,LayoutParams.WRAP_CONTENT);
 paramsd.height = 60;
 paramsd.width = 60;
 btn.setLayoutParams(paramsd);
 addContentView(btn,paramsd);

编辑:

import android.widget.Button;

添加按钮

Button b = new Button(this);
    b.setText("Button added dynamically!");
    b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    b.setId(MY_BUTTON);
    b.setOnClickListener(this);
    ll.addView(b);

EDIT2:
你可以创建LinearLayout linearLayout = new LinearLayout(this);并添加Button

Button btn = new Button(this);
 btn.setText("Just another button");
 linearLayout.addView(btn);

Edit3:另一个例子:

final Button button = new Button(this);
    button.setText("Press me!");
    setContentView(button);

    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });
网友评论