Как добавить кнопку на гистограмму без xml?

Я нарисовал гистограмму без xml. Мне нужно добавить кнопку под графиком. Может кто-нибудь предложить решение. Спасибо в Advance !! Следующее – моя программа.

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

А? Просто используйте XML. То, что у вас есть это пользовательское представление, не является препятствием для его использования. Ниже приведен краткий пример обычных виджетов Android в макете с пользовательским виджетами:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tileview="http://schemas.android.com/apk/res/net.rapacity.wizardry" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/sokostatus" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/sokoban_status"/> <net.rapacity.wizardry.TileView android:id="@+id/sokomaze" android:layout_width="fill_parent" android:layout_height="fill_parent" tileview:tileSize="32"/> </LinearLayout>