Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

android - Does changing the background also change the padding of a LinearLayout?

I have the following LinearLayout. What I don't understand is if I set the background to another image, the padding information are reset. Is there a way to prevent this?

<LinearLayout android:id="@+id/aPanel"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:background="@drawable/bkground"
    android:paddingLeft="15dp" android:paddingRight="15dp"> 

     <!-- some children here -->
     </LinearLayout>

I see the position of the children get shifted when I change the background drawable of the LinearLayout aPanel.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is the default behavior when changing the background Drawable on a View. According to Romain Guy, one of the Android developers, "The reason why setting an image resets the padding is because 9-patch images can encode padding." See his full answer in a similar question.

The fix is to reset the padding in code each time you change the background drawable.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...