Java 以编程方式在Android上绘制虚线

Java 以编程方式在Android上绘制虚线,java,android,android-layout,paint,Java,Android,Android Layout,Paint,我想在编程生成的文本视图之间绘制水平虚线。我尝试了以下代码: Paint fgPaintSel = new Paint(); fgPaintSel.setARGB(255, 0, 0, 0); fgPaintSel.setStyle(Paint.Style.STROKE); fgPaintSel.setPathEffect(new DashPathEffect(new float[]{5, 10}, 0)); 但什么也没发生。我只是复制并粘贴了这段代码。我应该怎样画一条虚线?谢谢。给活动布局一

我想在编程生成的文本视图之间绘制水平虚线。我尝试了以下代码:

Paint fgPaintSel = new Paint();
fgPaintSel.setARGB(255, 0, 0, 0);
fgPaintSel.setStyle(Paint.Style.STROKE);
fgPaintSel.setPathEffect(new DashPathEffect(new float[]{5, 10}, 0));

但什么也没发生。我只是复制并粘贴了这段代码。我应该怎样画一条虚线?谢谢。

给活动布局一个id。我在本次演示中使用了一个按钮和一个onclick处理程序PaintDashedLines()

在content_main.xml布局中

<LinearLayout android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" .../>

    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="PaintDashedLines"
        android:text="Press Me"/>
</LinearLayout>

-我的活动
--整数计数
--oncreate
--绘制虚线(视图v)
--公共静态可绘制CreateDashedLined()

在build.gradle中(尽管这不是一成不变的)


你不需要做任何其他事情

在drawable中创建
虚线.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line">

  <stroke
     android:color="#C7B299"
     android:dashWidth="10px"
     android:dashGap="10px"
     android:width="1dp"/>
</shape>
并设置其
背景

v.setBackgroundResource(R.drawable.dashed_line);

根据需要设置
视图的
高度
宽度

在可绘制文件夹中创建一个虚线.xml文件:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:left="-3px"
        android:right="-3px"
        android:top="-3px">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">

            <stroke
                android:width="2px"
                android:color="@color/dark_blue"
                android:dashGap="2px"
                android:dashWidth="3px" />
        </shape>
    </item>

</layer-list>
结果:


尝试调用canvas.drawPath(mPath,fgPaintSel);方法后this@NKushwah它无法解析
canvas
mPath
。谢谢。发布该类或方法的全部代码。@NKushwah我发布了全部代码。这就是我所有的绘图代码。您是否重写了onDraw()?谢谢您的代码。我使用的代码如下:1。注释掉文本视图行。2.将整个Java代码粘贴到Activity类的顶部。3.调用
View v=新视图(此);绘制虚线(v)我想要虚线的地方。但虚线不会出现。你能告诉我我做错了什么吗?谢谢。当我不评论TextView行时,我可以看到TextView和数字,但我看不到虚线。工作很有魅力,谢谢!有一个问题。当textView很短时,行也很短,我希望行始终保持全长。我怎样才能做到这一点?谢谢。@jason:1。设置视图的固定宽度,2。设置视图的最小宽度,3。设置视图的左侧和右侧填充,4。把它放在一个布局中,并在布局中添加虚线。最后一个问题:如何使点更像虚线?Thanksandroid:dashGap-更大的数字->行间距更大,dashWidth->单行宽度。希望有帮助。@jason有什么问题?你得不到输出还是什么?没有输出。
View v = new View(this);
v.setBackgroundResource(R.drawable.dashed_line);
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:left="-3px"
        android:right="-3px"
        android:top="-3px">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">

            <stroke
                android:width="2px"
                android:color="@color/dark_blue"
                android:dashGap="2px"
                android:dashWidth="3px" />
        </shape>
    </item>

</layer-list>
view.setBackground(getResources().getDrawable(R.drawable.dotted_line));