2011-07-14 9 views
0

ホーム画面のレイアウトに似たものを作成しようとしています。これは、Horizo​​ntalScrollViewに格納された単一の水平方向のLinearLayout内の複数の垂直方向のLinearLayoutsで構成されます。 Horizo​​ntalScrollViewを継承した 'HomeLayout'というカスタムクラスとして記述しました。Android:Horizo​​ntalScrollViewにレイアウトを追加する

hierarchyviewer

LinearLayout wrapper = new LinearLayout(getContext()); 
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
wrapper.setLayoutParams(params); 
wrapper.setOrientation(LinearLayout.HORIZONTAL); 

addView(wrapper); 

for(int i = 0; i < 2; i++) { 
    LinearLayout linear = (LinearLayout) View.inflate(getContext(), R.layout.screens, null);  
    linear.setLayoutParams(params); 

    wrapper.addView(linear); 
} 

問題 'はScreen2には' の幅を有する '0' で添加した場合です。

これは、onDraw()でこれを呼び出し、LayoutParams.FILL_PARENTではなくgetMeasuredWidth()およびgetMeasuredHeight()を使用する場合にのみ機能します。それでも、それが正しいのかどうかはわかりません。

さらに、onCreate()の 'wrapper'、 'screen1'、 'screen2'のビューを参照できません。私が間違っているのは何http://blog.velir.com/index.php/2010/11/17/android-snapping-horizontal-scroll/

は私が緩く、このリンクをたどっ?

答えて

0

これを参考にして、他の人に役立ちます。私は次のようにonMeasure()を実装して問題を解決しました:

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int width = MeasureSpec.getSize(widthMeasureSpec); 
    int height = MeasureSpec.getSize(heightMeasureSpec); 
    setMeasuredDimension(width, height); 

    for(int i = 0; i < mWrapper.getChildCount(); i++) { 
     View child = mWrapper.getChildAt(i); 
     child.setLayoutParams(new LinearLayout.LayoutParams(width, 
     LayoutParams.FILL_PARENT)); 
    } 

    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
} 
関連する問題