Android:如何获取视图组的大小尺寸?

时间:2013-10-31 12:11:36

标签: android layout view

我有一个gridlayout,我想在活动中显示任何内容之前检查它的大小,因为我想检查是否需要调整图像大小。不能这样做OnStart()因为显然还没有加载。由于与其余代码的同步问题,添加侦听器不起作用。如果我可以通过使用设备的尺寸手动生成值,那将是很好的,但我在生活中无法找到Android自动放置的那些愚蠢边距的确切值。有什么建议?基本上在下面的代码中,我需要调用GridLayout的高度/宽度,然后调用SplitImage()。

XML     

<GridLayout
    android:id="@+id/grid_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:useDefaultMargins="false" >

</GridLayout>

相关JAVA

public class GamePlay extends Activity {
int difficulty = 0;
int image = 0;
int widthView = 0;
int heightView = 0;
int boardW;
int boardH;
int nSqr;
Bitmap[][] gameBoard;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    Intent intent = getIntent();
    difficulty = intent.getIntExtra("DIFFICULTY_KEY_DMIROU246", 0);
    image = intent.getIntExtra("LEVEL_KEY_DMIROU246", 0);
    setContentView(R.layout.activity_game_play);
    Log.w("Derp", "1");

}

@Override
protected void onStart() {
    super.onStart();
    View view = (View) findViewById(R.id.grid_layout);
    Log.w("Derp", "2");
    heightView = view.getHeight();
    widthView = view.getWidth();
    Log.w("Derp", "3");
    gameBoard = SplitImage();
    Log.w("Derp", "9");
}

3 个答案:

答案 0 :(得分:1)

在屏幕上绘制视图之前,您不会获得视图的高度或宽度。 为此,您将有两种方法来获得布局的高度和宽度: -

1)ViewTreeObserver - Doc

    View view = (View) findViewById(R.id.grid_layout);
    ViewTreeObserver vto = view.getViewTreeObserver(); 
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
        @Override 
        public void onGlobalLayout() { 
            this.layout.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
            int width  = view.getMeasuredWidth();
            int height = view.getMeasuredHeight(); 
            gameBoard = SplitImage();
            Log.w("Derp", "9");
        } 
    });

2)Handler.postDelayed - Doc

    View view = (View) findViewById(R.id.grid_layout);
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

            @Override
            public void run() {
                int width  = view.getMeasuredWidth();
                int height = view.getMeasuredHeight(); 
                gameBoard = SplitImage();
                Log.w("Derp", "9");
            }
    }, 500);

答案 1 :(得分:0)

最简单的解决方案是异步执行此操作,使用view.getViewTreeObserver然后添加OnGlobalLayoutListener,以获得视图大小并调用splitImage()

请注意不要经常调用您的监听器,最好是在触发一次后将其删除。

答案 2 :(得分:0)

试试这个

问题:我有一些数据显示为列表(可扩展视图)。

主列表视图显示我正在使用列表视图并显示子视图显示另一个内部列表视图used.so子视图无法计算单元格高度。

1)我有UI TABLAT和PHONE

2)如果任何用户有任何问题实现时间这段代码请联系我,我会帮助你。“rp1741995@gmail.com”

    public static int getListViewHeightBasedOnChildren(ListView listView,Context context) {
         ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter != null)
        {
          int totalHeight = 0;
            int size = listAdapter.getCount();
            Log.e(TAG, "getListViewHeightBasedOnChildren: >>"+size);
            for (int i = 0; i < size; i++)
            {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }
            totalHeight = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount()-1));
            if(listAdapter.getCount()!=0) {
                if (MyApplication.isTablet(context)) {
                    Float height = convertDpToPixel(10);
                    totalHeight = (totalHeight / listAdapter.getCount()) + Math.round(height);
                }else {
                    Float height = convertDpToPixel(80);
                    totalHeight = (totalHeight / listAdapter.getCount()) + Math.round(height);
                }

            }
 ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight;
            //params.height;
             Log.e(TAG, "getListViewSize: "+String.valueOf(totalHeight));

             listView.setLayoutParams(params);
            return totalHeight;
        }
     return 0;
}