Tôi có bố cục tuyến tính đơn giản. Tôi thêm nút dynaically thông qua mã một dưới đây khác với một số không gian ở giữa. Tôi cần vẽ một đường thẳng đứng giữa hai nút đó (đường thẳng đứng đầu mũi tên cụ thể).Cách vẽ đường thẳng đứng giữa hai nút trong android
Bạn có thể vui lòng cho tôi biết cách vẽ một đường thẳng đứng từ mông nút1 đến đầu nút2 không.
Tôi đã sử dụng DrawLine() để vẽ một đường nhưng nó được vẽ bên dưới button2 với một số bù đắp.
Đây là mã của tôi:
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class SampleMethodActivity extends Activity {
Button b,b1;
public int width,height,bottom;
LinearLayout ll;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_method);
ll = (LinearLayout) findViewById(R.id.my_linear_layout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(0, 12, 0, 40);
b = new Button(this);
b.setText("This is a sample text");
b.setLayoutParams(params);
b.setGravity(Gravity.CENTER);
ll.addView(b);
b1 = new Button(this);
b1.setText("This is a sample text to chck the width and height of a button1 and need to check how long it gets stretched and to check the width");
b1.setLayoutParams(params);
b1.setGravity(Gravity.CENTER);
ll.addView(b1);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
width = b.getWidth();
height = b.getHeight();
bottom = b.getBottom();
DrawView dv = new DrawView(this,width/2,bottom);
ll.addView(dv);
}
}
Dưới đây là lớp DrawView:
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class DrawView extends View {
Paint paint = new Paint();
int x,y;
public DrawView(Context context,int x,int y) {
super(context);
this.x=x;
this.y=y;
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setColor(Color.WHITE);
paint.setStrokeWidth(3);
System.out.println("X: "+x);
canvas.drawLine(x, y, x, y+400, paint);
}
}
kiểm tra liên kết này: http://stackoverflow.com/questions/3809742/how-can-i-draw-a-vertical- line-in-an-activity-khi-a-button-is-pressed – Ranjit