2017-10-31 6 views
0

私はFrameLayoutを継承する単純なクラスAppPopupを持っています。クラスはコンテンツを膨張させてコンテンツ変数に格納します。コンテンツをポップアップビューの中央にする必要があります。私はonMeasureをオーバーライドしますが、コンテンツの高さ/幅が-1に等しい私は親ビューでそれを呼び出す子ビューはサイズ値を取得しません

public class AppPopup extends FrameLayout { 
    private View content; 
    private View bg; 
    public AppPopup(@NonNull Context context) { 
     super(context); 
    } 

    public AppPopup(Context context, @Nullable AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public void setLayout(int resId) { 
     LayoutInflater li=LayoutInflater.from(getContext()); 
     bg=li.inflate(R.layout.popup_bg_fade,null); 
     content=li.inflate(resId,null); 
    } 
    public void show() { 
     addView(bg); 
     addView(content); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     int heightSize = MeasureSpec.getSize(heightMeasureSpec); 
     int widthSize = MeasureSpec.getSize(widthMeasureSpec); 
     Log.d("layout",content.getLayoutParams().width+","+content.getLayoutParams().height+","+(content.getParent()==this)); -1,-1,true 
     content.setX(widthSize/2-content.getLayoutParams().width/2); 
     content.setY(heightSize/2-content.getLayoutParams().height/2); 
    } 

    @Override 
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
     super.onLayout(changed, left, top, right, bottom); 
     Log.d("layout",content.getLayoutParams().width+","+content.getLayoutParams().height+","+(content.getParent()==this)); //-1,-1,true 
    } 
} 

この方法では:

onLayoutサイズ内
AppPopup popup=new AppPopup(this); 
root.addView(popup); 
popup.setLayout(R.layout.gameover_popup1); 
popup.show(); 

も-1.But私ができる等しいです正しいサイズの画面で自分のビューを参照してください。 私は何かシンプルなものを見逃しているが、何が分からないのか分かっている。

答えて

0

int widthSize = MeasureSpec.getSize(widthMeasureSpec); WRAP_CONTENTまたはMATCH_PARENTにすることができます。

することはでき得るだけで(それが終えた後setsMeasuredだけなのでsuper.onMeasure後に動作します):あなたはsuper.onLayout child.getMeasuredWidth()の後に呼び出すことができ膨張後の子幅については

int widthSize = getMeasuredWidth(); 
int heightSize = getMeasuredHeight(); 

+0

私の質問は、子ビュー(コンテンツ変数)のサイズを取得する方法です。コンテンツをコンテナビュー内に配置する必要があります。コンテナサイズの検出に問題はありません。 – undefined

+0

onLayoutの後で、child.getMeasuredWidth()を呼び出すことができます。 –

+0

それでもコンテナのサイズが返されます。何らかの理由でビューの階層にコンテンツビューを追加すると、match_parentサイズのようにフルスクリーンに展開されますが、これは真です。サイズは固定です。ちょっとしたことで、コンテンツを追加のFrameLayoutこのルートレイアウトにwrap_contentを設定します。 – undefined

関連する問題