Android - How to restrict width of LinearLayout
I'm trying to restrict the width of a LinearLayout to 400dp or less. In other words, if the layout engine determines that the LinearLayout is going to be 320dp, I don't want to change anything, but if it determines it's going to be 480dp, I want to reduce开发者_运维技巧 it to 400dp.
I've tried everything I can think of to make this happen. My last attempt was to create a custom view inherited from LinearLayout and override the onMeasure method to set custom dimensions, however, the problem I'm running into is that even though the actual LinearLayout width is reduced all the views inside it (ie: buttons, etc.) are still laid out as if the width never changed, so they get cut off on the side.
If anyone has any suggestions on how to accomplish this or knows of any open source applications that do this that I could look at that would be much appreciated.
You should check getMeasuredWidth()
. If its bigger that you want, setLayoutParams()
with correct width. Remember that in Java units are in pixels, so you need to scale them properly.
精彩评论