视图的getHeight()总是返回零?

时间:2014-05-07 11:29:10

标签: android view android-softkeyboard rootview

我想检查屏幕上是否显示软键盘。为此我找到了一个代码并按照给出的方式进行了尝试。但activityRootView.getRootView().getHeight()activityRootView.getHeight()都返回零。 我需要你的专家观点。

代码:

LayoutInflater inflator5=   
(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View    row5 =     
inflator5.inflate(R.layout.order_confirmation_list2,null);



final LinearLayout activityRootView = (LinearLayout)row5. 
findViewById(R.id.orderconfirmRootId);


// calculate the difference between the root view height and the window view height
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();

// if more than 100 pixels, its probably a keyboard...
if (heightDiff > 100) {

// keyboard is visible, do something here

Toast toast = Toast.makeText(context, "keyboard visible!!!"   , Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();

} else {

// keyboard is not visible, do something here
       }

更新到这个问题:

我遵循了许多与此类型相关的答案,但是没有找到合适的.plz通过我新添加的代码。

当软键盘可见并隐藏时,如何在getview()方法内获得布局高度的差异?我在下面给出的代码中尝试了这个((EditText)view.findViewById(R.id.editTextQtyId))。addTextChangedListener。但总是通过getHeight()方法返回零 您的专家观点随时欢迎。

新代码:

@Override
public View getView(int i, final View view, final ViewGroup viewgroup) {
    row=view;
    holder=null;
    try
    {
        if(row == null)
        {

            Button RemoveOrderBtn;
            LayoutInflater inflator=
(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row=inflator.inflate(R.layout.add_to_orderlist2,viewgroup , false);
            holder=new MyViewHolder(row);
            row.setTag(holder);
            Log.d("newrows", "new row");

        }
        else
        {

        holder= (MyViewHolder) row.getTag();
        Log.d("recycling", "Recycling stuff");

        }
    }
    catch(Exception e)
    {
        e.printStackTrace();

    }


try
{

SingleRow temp=list.get(i);
holder.Dno.setText(temp.Dno);
holder.Size.setText(temp.size);
holder.Mrp.setText(temp.Mrp);
holder.Color.setText(temp.color);
holder.SingleOrderTotal.setText(temp.val_total);
holder.P_Category.setText(temp.Pcategory);
holder.editTextQtyId.setText(temp.val_count);
File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard.getAbsolutePath() + "/App" +temp.imageName);
FileInputStream streamIn;
  try {
    streamIn = new FileInputStream(file);
    Bitmap bitmap = BitmapFactory.decodeStream(streamIn); 
    holder.Image.setImageBitmap(bitmap);
    streamIn.close();

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
    }

catch(Exception e)
{
    e.printStackTrace();
}


 try
    {


     if(view!=null)
         {              
            ((EditText) view.findViewById(R.id.editTextQtyId)).setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View arg0, MotionEvent arg1) {

                    ((EditText) view.findViewById(R.id.editTextQtyId)).addTextChangedListener(new TextWatcher() {

                        @Override
                        public void afterTextChanged(Editable arg0) {

                        }

                        @Override
                        public void beforeTextChanged(CharSequence arg0, int arg1,
                                int arg2, int arg3) {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void onTextChanged(CharSequence arg0, int arg1,
                                int arg2, int arg3) {
                            // TODO Auto-generated method stub


                            LayoutInflater inflator5=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                            View    row5 = inflator5.inflate(R.layout.order_confirmation_list2,null);
                            final LinearLayout layout = (LinearLayout)row5. findViewById(R.id.orderconfirmRootId);
                             Log.d("Log", "Height: " + layout.getHeight());
                             Toast toast = Toast.makeText(context, "Height= "+layout.getHeight() , Toast.LENGTH_SHORT);
                             toast.setGravity(Gravity.CENTER, 0, 0);
                             toast.show();

                        }

                    });

                    return false;
                }
            });
         }

    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
return row;
}

1 个答案:

答案 0 :(得分:0)

你想用onCreate()方法获得身高吗?

视图尚未在onCreate()onStart()onResume()中构建。由于它们在技术上不存在(就ViewGroup而言),它们的尺寸为0。

您应该使用OnGlobalLayoutListener

这样的事情:

//parent
ViewTreeObserver vto = v.getViewTreeObserver();  
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  
            @SuppressWarnings("deprecation")
            @Override  
            public void onGlobalLayout() {  
                if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) 
                    v.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
                else
                    v.getViewTreeObserver().removeOnGlobalLayoutListener(this);


                View myView = v.findViewById(R.id.my_view);
                // here height is not 0
                int h = myView.getHeight();

            }  
        });