本文介绍了图高度返回0,我想从onGlobalLayout返回查看高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我的code返回的观点高度:
INT高度= 0;
公众诠释getViewHeight(最终查看V){
v.getViewTreeObserver()。addOnGlobalLayoutListener(
新ViewTreeObserver.OnGlobalLayoutListener(){ @燮pressLint(NewApi)
@燮pressWarnings(德precation)
@覆盖
公共无效onGlobalLayout(){
如果(Build.VERSION.SDK_INT< Build.VERSION_ codeS.JELLY_BEAN){
v.getViewTreeObserver()
.removeGlobalOnLayoutListener(本);
}其他{
v.getViewTreeObserver()
.removeOnGlobalLayoutListener(本);
}
高度= v.getMeasuredHeight();
//
Log.v(高度,v.getMeasuredHeight()+);
}
}); Log.v(高回报,v.getMeasuredHeight()+);
返回高度;
}
首先,它返回0则高度值的变化。
任何人都可以让我知道如何当它在onGlobalLayout测量返回视图高度。 return语句在overrided方法不工作。
或
有没有其他的办法让这通过传递视图实例返回视图高度的静态方法。
解决方案
***尝试这种方式*
我得到观的高度和宽度按查看**公共类测试扩展活动{私人RelativeLayout的布局;
私人诠释温度;
ViewTreeObserver VTO;
共享preferences共享preferences;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.test);
共享preferences = getShared preferences(拯救preF,
Context.MODE_PRIVATE); 布局=(RelativeLayout的)findViewById(R.id.layout);
TEMP =集(布局);
Log.e(温度,+共享preferences.getInt(钥匙,0));
}
私人诠释集(最终RelativeLayout的布局){
VTO = layout.getViewTreeObserver();
最后编辑器编辑=共享preferences.edit();
vto.addOnGlobalLayoutListener(新OnGlobalLayoutListener(){
@燮pressWarnings(德precation)
@覆盖
公共无效onGlobalLayout(){
。layout.getViewTreeObserver()removeGlobalOnLayoutListener(本); TEMP = layout.getMeasuredHeight();
editor.putInt(钥匙,温度);
editor.commit();
}
});
如果(共享preferences.contains(钥匙)){
Log.e(包含,+共享preferences.getInt(钥匙,0));
}
返回共享preferences.getInt(钥匙,0);
}}
Here is my Code to return Height of view:
int height = 0;
public int getViewHeight(final View v) {
v.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@SuppressLint("NewApi")
@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);
}
height = v.getMeasuredHeight();
//
Log.v(" height", v.getMeasuredHeight() + "");
}
});
Log.v(" return height", v.getMeasuredHeight() + "");
return height;
}
First It return 0. then the height value changes. Can anyone let me know how to return height of view when it measured in onGlobalLayout. Return statement not working in overrided method.
OR
Is there any other way to make a static method which return height of view by passing View instance.
解决方案
***try this way*
i get View Height and Width as per View**
public class Test extends Activity {
private RelativeLayout layout;
private int temp;
ViewTreeObserver vto;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
sharedPreferences = getSharedPreferences("savePref",
Context.MODE_PRIVATE);
layout = (RelativeLayout) findViewById(R.id.layout);
temp = set(layout);
Log.e("temp", "" + sharedPreferences.getInt("key", 0));
}
private int set(final RelativeLayout layout) {
vto = layout.getViewTreeObserver();
final Editor editor = sharedPreferences.edit();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
temp = layout.getMeasuredHeight();
editor.putInt("key", temp);
editor.commit();
}
});
if (sharedPreferences.contains("key")) {
Log.e("contains", "" + sharedPreferences.getInt("key", 0));
}
return sharedPreferences.getInt("key", 0);
}}
这篇关于图高度返回0,我想从onGlobalLayout返回查看高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!