我正在尝试以编程方式填充线性布局
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"
/>
答案 0 :(得分:30)
View v = new View(this);
v.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
5
));
v.setBackgroundColor(Color.parseColor("#B3B3B3"));
LinearLay.addView(v);
答案 1 :(得分: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)
答案 2 :(得分: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方法,添加一个绘制调用来自己画线
答案 3 :(得分:0)
你只需要为你的TextViews设置样式(背景),在这种情况下使视图成为浪费