android how to change width and height

前端 未结 6 1121
鱼传尺愫
鱼传尺愫 2020-12-04 19:42

I\'m making a for drawable.

I have my background image, and I want the second layer to be smaller, but it seems that doesn\'t matte

6条回答
  •  一生所求
    2020-12-04 20:16

    Unlike someone said, there are width and height attributes for as well. Thanks to @Entreco for posting a demonstration of that.
    android:width and android:height for Drawables are similar to android:layout_width and android:layout_height for Views, unlike they can be set to neither match_parent nor wrap_content, but you can achieve the same behavior of match_parent by setting the attribute android:gravity to either left|right or start|end (for matching parent's width), or top|bottom (for matching parent's height).

    Unfortunately, those attributes are only available starting from API level 23.

    However, considering the above method I suggested in place of match_parent and that android:width and android:height attributes are available for element (which has to be put inside a ) without the need of a newer API level, you could use a simple workaround:

    
    
        
    
            
    
                
    
                
            
        
    
        
    
            
        
    
    

    The above solution takes advantage of having a sized (with a transparent ) and an unsized one (with a ) that has android:gravity set to left|top|right|bottom for matching both parent's dimensions. So, the real size of the will be determined by that of the unique sized in the same parent.

    EDIT:

    Thanks to @Emil S for noticing that the above solution won't work when used as a window background. I can guess that at the time which system creates a window with the background specified on android:windowBackground attribute, no layout is performed as no process is started yet. So, Android will end up on rasterizing the drawable at the screen size and stretch it to fill the whole window, I think. This would explain how that could happen. Indeed, a possible solution that I thought of, but I haven't tested yet, which unfortunately would be supported only starting from API level 24, is to use a custom drawable as a window background. It is possible by referencing an XML resource that uses a custom drawable, like this. This way, the issue should be solved since Android is forced to start the app process before showing the window, and so the drawable should be correctly laid out since it's aware of the screen dimensions. That way, however, would imply a almost unnoticeable yet existing delay at the startup.

    EDIT:

    @João Carlos pointed out that my solution won't work (as it would cause a cyclic inheritance) when using an adaptive icon (those icons made by a background and a foreground, which support vector drawables). However, his point is a nonsense, because adaptive icons require API 26: one could use directly android:width and android:height on the splash screen or something (They just require API 23).
    So, in order to get anything to work in any version of Android, you'll need to differentiate two drawables, the former for API < 23, using the solution I posted, and the latter for API >= 23, using the two attributes as I said at the beginning of this post.

提交回复
热议问题