问题描述
我想我的Android应用转换成片段,支持多种屏幕尺寸,并正确使用新的ICS标签。 previously我使用了 onWindowFocusChanged()
方法和运行以下code在它的内部 - 基本上是这样做了我布置的一些动态格式被创建之后。
I am trying to convert my Android app to Fragments to support multiple screen sizes and to use the new ICS tabs correctly. Previously I used the onWindowFocusChanged()
method and ran the following code inside of it - basically this did some dynamic formatting of my layout after it was created.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LinearLayout theLayout = (LinearLayout)inflater.inflate(R.layout.tab_frag2_layout, container, false);
getWidthEditButton = (ImageButton) theLayout.findViewById(R.id.buttonEditPoints);
buttonAddPointsManual = (ImageView) theLayout.findViewById(R.id.buttonAddPointsManual);
linearPointsUsed = (LinearLayout) theLayout.findViewById(R.id.linearLayoutPointsUsed);
int paddingLeftForTracker = linearPointsUsed.getPaddingLeft();
int paddingRightForTracker = getWidthEditButton.getWidth();
linearPointsUsed.setPadding(paddingLeftForTracker, 0, paddingRightForTracker, 0);
}
现在,我已经搬到片段和出于某种原因,我paddingRightForTracker返回0。我遇到了一个问题,previously我在那里试图让宽度太早,因此我的举动onWindowFocusChanged previously,但不提供给片段。奇怪的是,居然paddingLeftForTracker返回一个非零值。
Now that I have moved to Fragments and for some reason my paddingRightForTracker returns 0. I ran into an issue previously where I was trying to get width too early, hence my move to onWindowFocusChanged previously, but that is not available to Fragments. The strange thing is that paddingLeftForTracker actually returns a non-zero value.
如果我手动设置paddingRightForTracker,变化发生,所以我知道的code上。只是想不通,为什么我的getWidth将返回0。
If I set paddingRightForTracker manually, the change takes place so I know the code is running. Just can't figure out why my getWidth is returning 0.
任何帮助将大大AP preciated。
Any help would be greatly appreciated.
推荐答案
您可以尝试做它onActivityCreated()。所以,你会保存到onCreateView这些观点的引用,然后访问他们onActivityCreated()。我认为,当你试图访问它,这就是为什么它没有返回宽度的观点没有完成创建。
You could try doing it in onActivityCreated(). So, you would save a reference to those views in onCreateView, and then access them in onActivityCreated(). I think the view isn't completed created when you're trying to access it, which is why it returns no width.
http://developer.android.com/reference/android/app/Fragment.html#onActivityCreated(android.os.Bundle)
好了,我发现了另一种方式来获得的宽度。我也一样,不能得到对双方都onViewCreated,onCreateView,也不onResume一个按钮的宽度。我发现这一点,试了一下,它的返回值,所以也许它会为你工作!
Ok, so I found out about another way to get the width. I, too, cannot get a button width on neither onViewCreated, onCreateView, nor onResume. I found this, tried it, and it's returning a value, so maybe it'll work for you!
ViewTreeObserver vto = button.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
width = button.getWidth();
height = button.getHeight();
}
});
仅供参考,我跑了onResume这个code,所以我不能完全肯定有什么地方它可以工作。
FYI, I ran this code in onResume, so I'm not exactly sure where else it could work.
这篇关于的getWidth返回片段0,getPaddingLeft返回非零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!