0

I defined a custom view with a public method:

public void setHeight(int height) {
    //this.getLayoutParams().height = height; --- NOT WORKING
    this.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, height)); // --- WORKING

When I use the comment out code it doesn't work (meaning that the view height is not changing when calling this method), but when I use the second phrase it's working as expected.

What can explain this behavior?

4

1 に答える 1

3

それは簡単です - のコードを見てくださいsetLayoutParams():

 public void setLayoutParams(ViewGroup.LayoutParams params) {
     if (params == null) {
         throw new NullPointerException("Layout parameters cannot be null");
     }
     mLayoutParams = params;
     if (mParent instanceof ViewGroup) {
         ((ViewGroup) mParent).onSetLayoutParams(this, params);
     }
     requestLayout();
 }

見る?requestLayout()レイアウトパラメータを設定した後に呼び出しています。

ところで、Android のソース コードはwww.grepcode.comで見つけることができ、多くの場合非常に役立ちます。

于 2013-02-24T09:56:55.667 に答える