android如何在onCreateView中获取高度和宽度

时间:2013-04-03 04:14:35

标签: android layout

我以编程方式创建我的视图,根本没有xml,需要我正在工作的区域的实际大小。我一直在使用此代码:

public static class DummySectionFragment extends Fragment {
   @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {

                if (container == null) {
                    return null;
                }

                Context context = getActivity();
                int tabNo = getArguments().getInt(ARG_SECTION_NUMBER);
                Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

                // check display size to figure out what image resolution will be loaded
                DisplayMetrics metrics = new DisplayMetrics();
                display.getMetrics(metrics);

                Point size = new Point();
                display.getRealSize(size);
                display.getSize(size);
                int width = size.x;
                int height = size.y;

但这并没有考虑到所有其他的装饰'比如标题栏和导航栏

文档说不使用显示指标来获取大小但是如何在onCreateView中找出视图的大小?我尝试了container.getHeight(),但返回0

我想问题是视图还没有被创建但是onCreateView必须有一个父知道,它知道视图有多大吗?

我怎么找到它?

感谢

仅供参考,这有效:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {

    if (container == null) {
        return null;
    }

    Context context = getActivity();
    int tabNo = getArguments().getInt(ARG_SECTION_NUMBER);
    Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

    // check display size to figure out what image resolution will be loaded
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    Point size = new Point();
    display.getRealSize(size);
    display.getSize(size);
    int width = size.x;
    int height = size.y;

    switch (metrics.densityDpi) {
        case DisplayMetrics.DENSITY_XXHIGH:     height -= 72;      break;
        case DisplayMetrics.DENSITY_XHIGH:      height -= 64;      break;
        case DisplayMetrics.DENSITY_HIGH:       height -= 48;      break;
        case DisplayMetrics.DENSITY_MEDIUM:     height -=  32;      break;
        case DisplayMetrics.DENSITY_LOW:        height -=  24;      break;
        default:
            Log.e("onCreateView", "Unknown density");
        }
    }

但它似乎并不是一种干净的方式。我猜到xhigh和xxhigh,我还没有找到实际数字

另外我应该补充说,视图是从MainActivity中的onTabSelected创建的,如下所示

@Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, show the tab contents in the
        // container view.
        Fragment fragment = new DummySectionFragment();

        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, tab.getPosition() + 1);
        fragment.setArguments(args);
        getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
    }

1 个答案:

答案 0 :(得分:3)

我想我有它

我认为getSize返回realsize减去导航栏,所以我需要减去操作栏

现在我这样做:

@Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {

            if (container == null) {
                return null;
            }

            Context context = getActivity();
            int tabNo = getArguments().getInt(ARG_SECTION_NUMBER);
            Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

            // check display size to figure out what image resolution will be loaded
            DisplayMetrics metrics = new DisplayMetrics();
            display.getMetrics(metrics);
            Point size = new Point();
            display.getSize(size);
            int width = size.x;
            int height = size.y;

            TypedValue tv = new TypedValue();
            int actionBarHeight=0;
            if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
            {
                actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
            }

            height -= actionBarHeight;

            //Log.d("MainActivity", "onCreateView tabNo="+tabNo +" display width="+width+" height="+height+" rotation="+rotation);

            switch(tabNo){
            case 1:
                return onCreateViewScoreFragment(context, width, height);
            case 2:
                return onCreateViewFilesFragment(context, inflater, width>height);
            default:
                return onCreateViewSettingsFragment(context, inflater, width>height);
            }
        } 

它适用于我的平板电脑,我不是100%肯定其他设备,我将不得不等待,看看我的客户说什么