3

I am dynamically creating some ImageViews inside a relativeLayout but I need the size to vary depending on the height and width of the screen. At the point where I set the height the views and layouts havent been created which means that the getHeight() and getWidth() are returning 0. I have looked at other threads on StackOverflow but none seem specific enough to my problem.

I am doing all this in a Fragment and would appreciate any guidance on how to solve this. Her is a small snippet of my code.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.fragment_hello_moon, parent, false);

    RelativeLayout rl = (RelativeLayout) v.findViewById(R.id.l1);
    tl.setBackgroundColor(Color.RED);

    ArrayList<ImageView> imgViews = new ArrayList<ImageView>();

    //Cant get height or width of parent as hasnt been created yet
    int numberOfBells = 16;
    int height = rl.getHeight() / numberOfBells;
    int width = rl.getWidth() / 2;

    final ImageView imageTopL = new ImageView(getActivity());

    if (true){
        imageTopL.setId(1);
        imageTopL.setImageResource(R.drawable.bell_dl_256);

        RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);

        params1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        params1.setMargins(50,10,0,10);

        imageTopL.setLayoutParams(params1);

        //Need the size to vary depending on the height/width
        imageTopL.getLayoutParams().height = height;
        imageTopL.getLayoutParams().width = width;

        imageTopL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imageTopL.setImageResource(R.drawable.bell_ul_256);
            }
        });

        imgViews.add(imageTopL);
    }
  for(ImageView i : imgViews){
        rl.addView(i);
    }

    return v;
}
4

5 回答 5

4

要获得屏幕的大小,您可以尝试:

 DisplayMetrics metrics = this.getResources().getDisplayMetrics();
  int width = metrics.widthPixels;
  int height = metrics.heightPixels;

看这个:以像素为单位获取屏幕尺寸

于 2013-10-28T16:10:09.683 回答
0

你可以使用得到高度和宽度

getViewTreeObserver().addOnPreDrawListener(new OnPreDarwListener() {
  .... run () {
   getHeight() + getWidth related stuff.
   getViewTreeObserver.removeOnPreDrawListener(this);
  }
});
于 2013-10-28T18:13:59.027 回答
0

请参阅getWidth() 和 getHeight() 始终返回的第一个答案0 。基本上,您必须等到第一次测量和布局通过之后,这直到之后才会发生onCreateView()并被onViewCreated()调用。

于 2013-10-28T17:55:59.560 回答
0

您正在尝试在实际测量视图并在屏幕上布局之前获取视图的大小(在 onCreateView 返回之前)。仅仅因为你膨胀视图并不意味着系统对尺寸有任何想法。尝试在 onCreateView 返回后计算尺寸,就像在 onActivityCreated 中一样。

于 2013-10-28T17:29:47.423 回答
0

要查找屏幕的高度和宽度,您可以尝试以下操作:

DisplayMetrics metrics = new DisplayMetrics();   //for all android versions
getWindowManager().getDefaultDisplay().getMetrics(metrics); 
int Measuredwidth  = metrics.widthPixels;       
int Measuredheight = metrics.heightPixels;
于 2014-01-17T07:26:26.843 回答