【问题标题】:Relative Layout get Width/Height return 0相对布局获取宽度/高度返回 0
【发布时间】:2013-10-16 20:32:12
【问题描述】:

我需要获取相对布局的宽度和高度,但是我不知道相对布局何时准备好为我提供这两个属性。我的活动有几个选项卡,我在onCreateView() 中调用inflater.inflate(R.layout.fragment_pizza, container, false); 之后的getWidth() 和getHeight(),但是它们都返回0,因此它们显然还没有准备好。是否有某种事件,如 afterCreateView() 或类似的事件,我可以调用这两个方法并获取实际的宽度和高度?

代码:

public class PizzaFragment extends Fragment {

    private static final String TAG = "PizzaFragment";

    private Controller controller;

    private static final int BUTTON_WIDTH = 70;
    private static final int BUTTON_HEIGHT = 20;

    private static final int MARGIN_LEFT = 80;
    private static final int MARGIN_BOTTOM = 30;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        controller = Controller.getInstance();
        controller.loadProducts("pizza", getActivity().getApplicationContext());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_pizza, container, false);

        RelativeLayout layout = (RelativeLayout) view.findViewById(R.id.rl_order_pizza);
        Log.d(TAG, "Width: " + layout.getWidth());
        Log.d(TAG, "Height: " + layout.getHeight());

        int currentX = 10;
        int currentY = 10;

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(BUTTON_WIDTH, BUTTON_HEIGHT);


        for (Product product: controller.getProducts("pizza")){

            Button tempButton = new Button(getActivity());
            tempButton.setId(product.getId());
            tempButton.setText(product.getName());

            if (layout.getWidth() < currentX + MARGIN_LEFT){
                currentX = 10;
                currentY = MARGIN_BOTTOM;

            }
            else{
                currentX += MARGIN_LEFT;
            }

            Log.d(TAG, "CurrentY: " + currentY);
            Log.d(TAG, "CurrentX: " + currentX);

            layoutParams.leftMargin = currentX;
            layoutParams.bottomMargin = currentY;

            tempButton.setLayoutParams(layoutParams);

            layout.addView(tempButton);
        }

        return view;

    }
}

【问题讨论】:

标签: android android-relativelayout


【解决方案1】:

我的建议是在onCreateView 中使用这样的全局布局监听器:

YOUR_LAYOUT_INSTANCE = view.findViewById(R.id.YOUR_LAYOUT_ID);

...

YOUR_LAYOUT_INSTANCE.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
    @Override
    public void onGlobalLayout()
    {
        // gets called after layout has been done but before display.
        if (Build.VERSION.SDK_INT < 16) {
            YOUR_LAYOUT_INSTANCE.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        } else {
            YOUR_LAYOUT_INSTANCE.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
        // get width and height
    }
});

【讨论】:

    【解决方案2】:

    addOnGlobalLayoutListener 已弃用 removeOnGlobalLayoutListener 。为了更好地处理扩展上述由 @fasteque

    提供的解决方案
        YOUR_LAYOUT_INSTANCE.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
        {
            @Override
            public void onGlobalLayout()
            {
                // gets called after layout has been done but before display.
                if (Build.VERSION.SDK_INT < 16) {
    
                            YOUR_LAYOUT_INSTANCE.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        } 
                else {
                            YOUR_LAYOUT_INSTANCE.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                        }
    
            // get width and height
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多