Java 如何以编程方式在Android中制作水平线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21098618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:34:17  来源:igfitidea点击:

How to make horizontal line in Android programmatically

javaandroid

提问by Deron

I am trying to fill linear layout programmatically like this

我正在尝试像这样以编程方式填充线性布局

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

and I want to add (after every TextView (like kac)) a horizontal line like this one I have in my xml

我想添加(在每个 TextView(如 kac)之后)一条水平线,就像我在我的 xml 中

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

采纳答案by vipul mittal

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

LinearLay.addView(v);

回答by trippedout

edit* example:

编辑*示例:

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

if i understand that you are just looking to draw a line after every textview, your best bet is probably to just extend TextView with a custom class, overriding the onDraw method, adding a paint call to draw the line yourself

如果我知道您只是想在每个 textview 后画一条线,那么最好的办法可能是使用自定义类扩展 TextView,覆盖 onDraw 方法,添加一个绘制调用来自己画线

回答by Andrew Matuk

you just need to set style (background) to your TextViews, making Views to in this case is a waste

你只需要为你的 TextViews 设置样式(背景),在这种情况下制作 Views 是一种浪费

回答by balysv

I recommend creating a drawable containing the line and setting it as a background to your textview. You would avoid unnecessary views.

我建议创建一个包含线条的可绘制对象并将其设置为文本视图的背景。您将避免不必要的视图。

Sample drawable could be:

可绘制样本可能是:

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

And you can set it by

你可以设置它

textView.setBackgroundResource(R.drawable.resource)