2014-01-13 36 views
7

我试图填补线性布局编程这样如何使水平线的Android编程

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
      LayoutParams.WRAP_CONTENT,  
      LayoutParams.WRAP_CONTENT 
    ); 
    params.setMargins(dpToPx(6), 0, 0, 0); 
    kac.setLayoutParams(params); 
    LinearLay.addView(kac); 

,我想添加(每TextView中后(如KAC))一个水平线上像这样的我在我的XML

<View 
      android:layout_width="fill_parent" 
      android:layout_height="1dip" 
      android:background="#B3B3B3" 
      android:padding="10dp" 
      /> 

回答

26
View v = new View(this); 
v.setLayoutParams(new LinearLayout.LayoutParams(
     LayoutParams.MATCH_PARENT,  
     5 
)); 
v.setBackgroundColor(Color.parseColor("#B3B3B3")); 

LinearLay.addView(v); 
+0

感谢VIPUL设置它,但你可以请你告诉我,我们才能在底部设置该水平线的重力父布局。如果是,那么我们如何才能做到这一点。 –

1

编辑*例如:

@Override 
protected void onDraw(Canvas canvas) 
{ 
    //this would draw the textview normally 
    super.onDraw(canvas); 

    //this would add the line that you wanted to add at the bottom of your view 
    //but you would want to create your rect and paint outside of the draw call 
    //after onmeasure/onlayout is called so you have proper dimensions 
    canvas.drawRect(new Rect(0, this.getHeight()-1, this.getWidth(), this.getHeight()), mPaint); 
} 

如果我明白,你只是希望借鉴每次的TextView后一条线,你最好的选择可能是只是延长TextView中使用自定义类,重写的onDraw方法,增加油漆的呼叫,那条自己

0

你只需要设置样式(背景)到你的TextViews,在这种情况下使视图是浪费

2

我建议创建一个包含该行的drawable并将其设置为您的textview的背景。你会避免不必要的意见。

样品绘制的可能是:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape android:shape="rectangle"> 
      <solid android:color="#LINE_COLOR"/> 
      <padding 
       android:bottom="1dp" 
       /> 
     </shape> 
    </item> 
    <item> 
     <shape android:shape="rectangle"> 
      <solid android:color="#BACKGROUND_COLOR"/> 
     </shape> 
    </item> 
</layer-list> 

,您可以通过

textView.setBackgroundResource(R.drawable.resource)