Vertical line using XML drawable

前端 未结 15 719
无人及你
无人及你 2020-11-27 09:57

I\'m trying to figure out how to define a vertical line (1dp thick) to be used as a drawable.

To make a horizontal one, it\'s pretty straightforward:



        
15条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 10:11

    You can nest your shape inside a rotate tag.

    
        
            
        
    
    

    However, the only problem is the layout params defined in your layout xml will be the dimensions used to draw the original shape. Meaning if you want your line to be 30dp tall, you need to define a layout_width of 30dp in your layout xml. But the final width will also be 30dp in that case, which is likely undesirable for most situations. This essentially means both width and height have to be the same value, the value of your desired length for the line. I couldn't figure out how to fix this.

    This seems to be the "android way" solution, but unless there's some fix or workaround for the dimensions issue I mention then this likely won't work for most people. What we really need is an orientation attribute in or .

    You can also try referencing another drawable in the rotate tag's attributes, such as:

    
    

    However I haven't tested this and expect it to have the same issues.

    -- EDIT --

    Oh, I actually figured out a fix. You can use a negative margin in your layout xml to get rid of the undesired extra space. Such as:

    
    

提交回复
热议问题