问题
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