0

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

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");
}
4

3 回答 3

3

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

1) ViewTreeObserver——文档

    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--文档

    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);
于 2013-10-31T12:26:41.053 回答
0

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

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

于 2013-10-31T12:19:03.870 回答
0

试试这个

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

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

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;
}
于 2018-06-04T09:52:36.207 回答