Can anybody tell how to draw a line in Android, perhaps with an example?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
or if you just want a line
If you're working with
ConstraintLayout
you need to define at least 2 constraints for the line to show up. Like this:Though I defined 3 constraints.
This will create a straight horizontal line, hope it helps!.
If you want to have a simple Line in your Layout to separate two views you can use a generic View with the height and width you want the line to have and a set background color.
With this approach you don't need to override a View or use a Canvas yourself just simple and clean add the line in xml.
The example code I provided will generate a line that fills the screen in width and has a height of one dp.
If you have problems with the drawing of the line on small screens consider to change the height of the line to px. The problem is that on a ldpi screen the line will be 0.75 pixel high. Sometimes this may result in a rounding that makes the line vanish. If this is a problem for your layout define the width of the line a ressource file and create a separate ressource file for small screens that sets the value to 1px instead of 1dp.
This approach is only usable if you want horizontal or vertical lines that are used to divide layout elements. If you want to achieve something like a cross that is drawn into an image my approach will not work.
You can draw multiple straight lines on view using Finger paint example which is in Developer android. example link
Just comment:
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
You will be able to draw straight lines.for horizontal line on the layout :
for vertical line on the layout :