在android中创建水平和垂直虚线
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20583298/
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
Creating horizontal and vertical dotted lines in android
提问by Prasanth S
I want to draw horizontal and vertical dotted lines in android using shapes.
我想使用形状在android中绘制水平和垂直虚线。
I want to draw like this
我想画成这样
For Horizontal line
对于水平线
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<stroke
android:dashGap="6px"
android:dashWidth="6px"
android:color="#C7B299" />
</shape>
For vertical line
对于垂直线
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<size
android:height="400dp"/>
<stroke
android:dashGap="6px"
android:dashWidth="6px"
android:color="#C7B299" />
</shape>
But vertical dotted line not displaying my output shows like this
但是垂直虚线不显示我的输出显示如下
How to draw vertical line.
竖线怎么画。
回答by
I found the solution
我找到了解决方案
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="90"
android:toDegrees="90" >
<shape android:shape="line" >
<stroke
android:dashGap="6px"
android:dashWidth="6px"
android:color="#C7B299" />
</shape>
</rotate>
OR
或者
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="90"
android:toDegrees="90"
android:drawable="@drawable/horizontal_line"/>
回答by ulix
I think I've found a "cleaner" solution for this problem by creating a custom view containing specific code to draw the dashed lines (in both vertical and horizontal orientations), and a bunch of attributes to make it very easy to use it from XML layouts. The main advantage of this approach over the "rotated line" method is that you can set the size of the dashed line view the way you would normally do, without having to worry about how the view is going to behave after the rotation (once the rotation applies to the entire dashed line view and not only to the line being drawn).
我想我已经找到了一个“更干净”的解决方案来解决这个问题XML 布局。与“旋转线”方法相比,这种方法的主要优点是您可以按照通常的方式设置虚线视图的大小,而不必担心旋转后视图的行为(一旦旋转适用于整个虚线视图,而不仅适用于正在绘制的线)。
So here is the step by step solution:
所以这里是一步一步的解决方案:
Create the file "/res/values/attrs.xml" with the following contents:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="DividerView"> <attr name="color" format="color" /> <attr name="dashLength" format="dimension" /> <attr name="dashGap" format="dimension" /> <attr name="dashThickness" format="dimension" /> <attr name="orientation" format="enum"> <enum name="horizontal" value="0" /> <enum name="vertical" value="1" /> </attr> </declare-styleable> </resources>
使用以下内容创建文件“/res/values/attrs.xml”:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="DividerView"> <attr name="color" format="color" /> <attr name="dashLength" format="dimension" /> <attr name="dashGap" format="dimension" /> <attr name="dashThickness" format="dimension" /> <attr name="orientation" format="enum"> <enum name="horizontal" value="0" /> <enum name="vertical" value="1" /> </attr> </declare-styleable> </resources>
This creates the attributes to control the custom view. Note: If the file above already exists in your project, just copy/paste the "declare-stylable" block inside the existing "resources" block.
这将创建用于控制自定义视图的属性。注意:如果上面的文件已经存在于您的项目中,只需将“declare-stylable”块复制/粘贴到现有的“resources”块中。
Create the class DividerView and paste the contents below:
public class DividerView extends View { static public int ORIENTATION_HORIZONTAL = 0; static public int ORIENTATION_VERTICAL = 1; private Paint mPaint; private int orientation; public DividerView(Context context, AttributeSet attrs) { super(context, attrs); int dashGap, dashLength, dashThickness; int color; TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.DividerView, 0, 0); try { dashGap = a.getDimensionPixelSize(R.styleable.DividerView_dashGap, 5); dashLength = a.getDimensionPixelSize(R.styleable.DividerView_dashLength, 5); dashThickness = a.getDimensionPixelSize(R.styleable.DividerView_dashThickness, 3); color = a.getColor(R.styleable.DividerView_color, 0xff000000); orientation = a.getInt(R.styleable.DividerView_orientation, ORIENTATION_HORIZONTAL); } finally { a.recycle(); } mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setColor(color); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(dashThickness); mPaint.setPathEffect(new DashPathEffect(new float[] { dashLength, dashGap, }, 0)); } public DividerView(Context context) { this(context, null); } @Override protected void onDraw(Canvas canvas) { if (orientation == ORIENTATION_HORIZONTAL) { float center = getHeight() * .5f; canvas.drawLine(0, center, getWidth(), center, mPaint); } else { float center = getWidth() * .5f; canvas.drawLine(center, 0, center, getHeight(), mPaint); } } }
In order to use auto-complete of attributes on your layout files, add the following name space definition on the topmost container:
xmlns:custom="http://schemas.android.com/apk/res/com.example"
创建类 DividerView 并粘贴以下内容:
public class DividerView extends View { static public int ORIENTATION_HORIZONTAL = 0; static public int ORIENTATION_VERTICAL = 1; private Paint mPaint; private int orientation; public DividerView(Context context, AttributeSet attrs) { super(context, attrs); int dashGap, dashLength, dashThickness; int color; TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.DividerView, 0, 0); try { dashGap = a.getDimensionPixelSize(R.styleable.DividerView_dashGap, 5); dashLength = a.getDimensionPixelSize(R.styleable.DividerView_dashLength, 5); dashThickness = a.getDimensionPixelSize(R.styleable.DividerView_dashThickness, 3); color = a.getColor(R.styleable.DividerView_color, 0xff000000); orientation = a.getInt(R.styleable.DividerView_orientation, ORIENTATION_HORIZONTAL); } finally { a.recycle(); } mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setColor(color); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(dashThickness); mPaint.setPathEffect(new DashPathEffect(new float[] { dashLength, dashGap, }, 0)); } public DividerView(Context context) { this(context, null); } @Override protected void onDraw(Canvas canvas) { if (orientation == ORIENTATION_HORIZONTAL) { float center = getHeight() * .5f; canvas.drawLine(0, center, getWidth(), center, mPaint); } else { float center = getWidth() * .5f; canvas.drawLine(center, 0, center, getHeight(), mPaint); } } }
为了在布局文件上使用自动完成属性,请在最顶层的容器中添加以下命名空间定义:
xmlns:custom="http://schemas.android.com/apk/res/com.example"
Replace com.example
by the name of your package. You can also change custom
by any prefix that better suits your needs.
Note: You may need to restart Eclipse to get the auto-complete working after changes on attrs.xml file.
替换com.example
为您的包的名称。您还可以更改custom
更适合您需要的任何前缀。注意:您可能需要重新启动 Eclipse 才能在 attrs.xml 文件更改后自动完成工作。
And finally create your dashed lines by inserting the following element on your layout, like any other view:
<com.example.DividerView android:layout_width="1dp" android:layout_height="fill_parent" android:layerType="software" custom:color="@color/grey" custom:orientation="vertical" custom:dashLength="1dp" custom:dashGap="1dp" custom:dashThickness="1dp" />
最后通过在布局中插入以下元素来创建虚线,就像任何其他视图一样:
<com.example.DividerView android:layout_width="1dp" android:layout_height="fill_parent" android:layerType="software" custom:color="@color/grey" custom:orientation="vertical" custom:dashLength="1dp" custom:dashGap="1dp" custom:dashThickness="1dp" />
I hope it helps!
我希望它有帮助!
回答by Sfseyhan
If the View has 1dp width then just rotating your horizontal line is not enough. The vertical line's length will be 1dp as it is drawn horizontally first, then rotated. Here is a trick to solve this problem:
如果视图的宽度为 1dp,那么仅旋转水平线是不够的。垂直线的长度将为 1dp,因为它首先水平绘制,然后旋转。这里有一个技巧可以解决这个问题:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="-300dp"
android:right="-300dp">
<rotate
android:drawable="@drawable/dash_line_divider_horizontal"
android:fromDegrees="90"
android:toDegrees="90"/>
</item>
</layer-list>
回答by Simon Thomas
This works for me:
这对我有用:
vertical_line.xml
垂直线.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@android:color/transparent"/>
<stroke
android:width="1px"
android:color="#60000000"
android:dashGap="5px"
android:dashWidth="5px" />
</shape>
In Layout:
在布局中:
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:background="@drawable/vertical_line" />
回答by Job M
This one solves the problem nicely
Create drawable line_dash.xml
这个很好的解决了问题 Create drawable line_dash.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="-1dp"
android:left="-1dp"
android:right="-1dp"
android:top="0dp">
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="@color/grey_20"
android:dashGap="3dp"
android:dashWidth="3dp" />
<solid android:color="@android:color/transparent" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
</item>
</layer-list>
Use it like this
像这样使用它
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_margin="@dimen/spacing_middle"
android:background="@drawable/line_dash" />
回答by Sagar Wankhede
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="90"
android:toDegrees="90">
<shape android:shape="line">
<stroke
android:color="@color/darkGray"
android:width="1dp"
android:dashGap="4dp"
android:dashWidth="2dp"/>
</shape>
</rotate>
<View
android:layerType="software"
android:background="@drawable/bg_vertical_dash_gray_1dp"
android:layout_width="@dimen/_15sdp"
android:layout_height="@dimen/_30sdp"/>
The key for the above code to get working is using android:layerType="software"
.
For more information, check thislink.
上述代码开始工作的关键是使用android:layerType="software"
. 有关更多信息,请查看此链接。
回答by Saiful Islam Sajib
This solution is 100% working and wish help you:
此解决方案 100% 有效,希望对您有所帮助:
At first create a drawable which will draw a horizontal dashed line.
首先创建一个drawable,它将绘制一条水平虚线。
Let dashed line drawable name is horizontal_dashed_line.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke
android:width="3dp"
android:color="#80ffffff"
android:dashWidth="20dp"
android:dashGap="5dp" />
</shape>
if you want to have vertical dashed line you have to rotate this drawable by followings:
如果你想有垂直的虚线,你必须通过以下方式旋转这个drawable:
Let drawable name is vertical_dashed_line.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="90"
android:toDegrees="90"
android:pivotX="50%"
android:pivotY="50%"
android:drawable="@drawable/horizontal_dashed_line">
</rotate>
Now you have a horizontal and vertical dashed line.
现在你有一条水平和垂直的虚线。
How to use:
如何使用:
To draw horizontal line simply add horizontal_dashed_line.xml in your layout. For example:
要绘制水平线,只需在布局中添加 horizontal_dashed_line.xml。例如:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/horizontal_dashed_line"
</RelativeLayout>
But if you want vertical line, just add vertical_dashed_line.xml instead of horizontal_dashed_line.xml.For example:
但是如果你想要竖线,只需添加vertical_dashed_line.xml而不是horizontal_dashed_line.xml。例如:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/vertical_dashed_line"
</RelativeLayout>
Good luck!
祝你好运!
回答by jmorales
For the vertical line:
对于垂直线:
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="90"
android:toDegrees="90">
<shape
android:shape="line">
<stroke
android:width="2dp"
android:color="#ff00ff"
android:dashWidth="8dp"
android:dashGap="5dp" />
<size android:width="120dp" />
</shape>
</rotate>
For the horizontal line:
对于水平线:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android">
<shape
android:shape="line">
<stroke
android:width="2dp"
android:color="@color/accent_color"
android:dashWidth="3dp"
android:dashGap="2dp" />
</shape>
</rotate>