简体   繁体   English

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

[英]Drawing dashed line on Android programmatically

I want to draw horizontal dashed lines between programmatically generated TextViews. 我想在编程生成的TextViews之间绘制水平虚线。 I tried this code : 我试过这段代码:

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

But nothing happened. 但什么都没发生。 I just copied and pasted this code. 我只是复制并粘贴了这段代码。 What should I do to draw a dashed line? 我该怎么画一条虚线? Thanks. 谢谢。

Give the Activity Layout an id. 为活动布局指定一个ID。 I've used a button for this demonstration with an onclick handler PaintDashedLines(). 我使用onLlick处理程序PaintDashedLines()使用了一个按钮进行演示。

In the content_main.xml layout. 在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>

Used a static int to count for purposes of demo, with a separate method to create the drawable, for modularisation. 使用静态int来计算demo的目的,使用单独的方法来创建drawable,用于模块化。

In your activity: 在您的活动中:

static int tvCount = 0;

public void PaintDashedLines(View v) {
    LinearLayout ll = (LinearLayout) findViewById(R.id.main);
    TextView tv = new TextView(MainActivity.this);
    tv.setGravity(Gravity.CENTER);
    tv.setTextSize(25);
    tv.setPadding(0, 5, 0, 5);
    ll.addView(tv);
    tv.setText("TextView " + tvCount);
    ImageView divider = new ImageView(MainActivity.this);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            ll.getWidth(), 2);
    lp.setMargins(0, 5, 0, 5);
    divider.setLayoutParams(lp);
    divider.setBackground(CreateDashedLined());
    ll.addView(divider);
    tvCount++;
}

public static Drawable CreateDashedLined() {
    ShapeDrawable sd = new ShapeDrawable(new RectShape());
    Paint fgPaintSel = sd.getPaint();
    fgPaintSel.setColor(Color.BLACK);
    fgPaintSel.setStyle(Paint.Style.STROKE);
    fgPaintSel.setPathEffect(new DashPathEffect(new float[]{5, 10}, 0));
    return sd;
}

在此输入图像描述

-MyActivity -MyActivity
--int count; --int count;
--oncreate --oncreate
--PaintDashedLines(View v) --PaintDashedLines(查看v)
--public static Drawable CreateDashedLined() --public static Drawable CreateDashedLined()

In the build.gradle (though this is not set in stone) 在build.gradle中(虽然这不是一成不变的)

    minSdkVersion 18
    targetSdkVersion 23

You do not need to do anything else. 你不需要做任何其他事情。

Create a dotted_line.xml file in drawable folder: 在drawable文件夹中创建dotted_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>

Add this drawable as background: 添加此drawable作为背景:

view.setBackground(getResources().getDrawable(R.drawable.dotted_line));

Result: 结果:

在此输入图像描述

create dashed_line.xml in drawable 在drawable中创建dashed_line.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>

Programmatically add View 以编程方式添加View

View v = new View(this);

and set its background 并设置其background

v.setBackgroundResource(R.drawable.dashed_line);

set height and width of your View according to your need. 根据需要设置View heightwidth

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM