我在一个FrameLayout中有多个ScrollView。在其中一个FrameLayout中,我正在加载一个PreferenceFragment。现在,我需要将FrameLayout的高度设置为PreferenceFragment的高度,以避免嵌套滚动( PrefrenceFragment也可以滚动)。
怎样才能得到PreferenceFragment的高度
我试图在onCreateView()类中重写PreferenceFragment,并调用View.getHeight(),但它只是返回0。
将FrameLayout设置为wrap_content,也不起作用,它只是占用了单个偏好的空间.
编辑:我现在尝试通过使用addOnGlobalLayoutListener()获得高度,但是返回的值只是显示的高度,而不是PreferenceFragment的全部高度。因此,我只是得到了wrap_content的一个首选项的高度,或者例如,如果我将FrameLayout的高度设置为300,则为300。我需要所有偏好的全部高度(不管是不是)。
@Override
public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle)
{
final View v = super.onCreateView(paramLayoutInflater, paramViewGroup, paramBundle);
if (v != null) {
ViewTreeObserver vto = v.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Log.d("TEST", "Height = " + v.getHeight() + " Width = " + v.getWidth());
ViewTreeObserver obs = v.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
}
});
}
return v;
}发布于 2016-01-18 18:43:42
您应该在活动中等待WindowFocusChange:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus)
//LAYOUT FRAGMENT NOW
}发布于 2016-01-18 18:49:54
尝尝这个
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
height = parentLayout.getheight();// the root layout of the fragment
//TODO:
}
}, 200);https://stackoverflow.com/questions/34861195
复制相似问题