Android - getHeight() and getWidth()

六眼飞鱼酱① 提交于 2019-12-11 00:16:59

问题


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;
}

回答1:


to get the size of the screen you can try:

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

Look this: Get screen dimensions in pixels




回答2:


You are trying to get the size of the views before they are actually measured and layed out on the screen (before onCreateView returns). Just because you inflate the view does not mean the system has any idea about the sizes. Try calculating the sizes after onCreateView returns like in onActivityCreated.




回答3:


See the first answer to getWidth() and getHeight() always returning 0. Basically, you have to wait until after the first measure and layout pass, which doesn't happen until after onCreateView() and onViewCreated() have been called.




回答4:


you can get height and width using

getViewTreeObserver().addOnPreDrawListener(new OnPreDarwListener() {
  .... run () {
   getHeight() + getWidth related stuff.
   getViewTreeObserver.removeOnPreDrawListener(this);
  }
});



回答5:


To find height and width of screen, you can try this:

DisplayMetrics metrics = new DisplayMetrics();   //for all android versions
getWindowManager().getDefaultDisplay().getMetrics(metrics); 
int Measuredwidth  = metrics.widthPixels;       
int Measuredheight = metrics.heightPixels;


来源:https://stackoverflow.com/questions/19639691/android-getheight-and-getwidth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!