2013-09-23 41 views
5

我有一個簡單的線性佈局。我通過代碼之下動態地添加按鈕,其間有一些空間。我需要在這兩個按鈕之間畫一條垂直線(具體的垂直箭頭標題)。如何在android中的兩個按鈕之間繪製垂直線

請你讓我知道如何從button1的buttom畫一條垂直線到button2的頂端。

我使用DrawLine()來繪製一條線,但它得到繪製在button2下面的一些偏移量。

這裏是我的代碼:

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

    } 
} 

下面是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); 
    } 

} 
+0

檢查此鏈接:http://stackoverflow.com/questions/3809742/how-can-i-draw-a-vertical-線路輸入-AN-活動的時-A-按鈕被按壓 – Ranjit

回答

0

要創建一個行添加這colors.xml

<color name="streat_line">#D0D5D8</color> 

應用此

<TextView 
android:id="@+id/TextView" 
android:layout_width="1dp" 
android:layout_height="wrap_content" 
android:background="@color/streat_line" > 
3

繪製與景觀

線路如果你只需要一個直水平或垂直線,那麼最簡單的方式,可能是隻使用在XML佈局文件視圖。你會做這樣的事情:

<View 
    android:layout_width="1dp" 
    android:layout_height="match_parent" 
    android:background="@android:color/black" /> 

設置第一個按鈕gravity to leftto right並在它們之間在他們之間增加一個視圖,設置它的gravity to center

第二種方式

添加按鈕(重量45)圖(10)BUTTON2(weight45)和你做。 並且您還可以創建一個xml並將其充氣到LinearLayout。

對於垂直線寬度1DP和水平線高度1DP