How to draw a line on the LinearLayout?

别等时光非礼了梦想. 提交于 2019-12-11 14:07:52

问题


Do you know how to draw line on the LinearLayout? To create a LinearLayout, I am using a xml resource:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
    LinearLayout linear = (LinearLayout)findViewById(R.id.linearlayout);
    //I wanna draw line on "linear"
}

Is it possible ?


回答1:


There are many ways; one way is to define a ShapeDrawable representing the line in XML (in your res folder) and set that as the background resource of say, an ImageView inside the LinearLayout.

You can read more about drawables and 2D graphics in general in the docs.

Feel free to provide some more context (specific use case) so I could elaborate on this answer or others could answer.




回答2:


I created an image in an image editing tool that was a 2x2 pixel square of the color I wanted. Then in code I created an ImageView and set that image as the source. Lastly I set the ImageView height to FitContent and the width to FillParent and that gave me a horizontal line.



来源:https://stackoverflow.com/questions/1951213/how-to-draw-a-line-on-the-linearlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!