我正在尝试将我的Android应用程序转换为Fragments以支持多个屏幕尺寸并正确使用新的ICS选项卡。之前,我使用onWindowFocusChanged()
方法并在其中运行下面的代码 - 基本上在创建布局后对其进行了一些动态格式化。
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);
}
现在我已经转移到Fragments,但是我的paddingRightForTracker返回0。以前我遇到过一个问题,那时我试图过早地获取宽度,因此我之前转移到onWindowFocusChanged,但是这对于Fragments不可用。奇怪的是,paddingLeftForTracker实际上返回了一个非零值。
如果我手动设置paddingRightForTracker,更改就会发生,所以我知道代码正在运行。只是无法弄清楚为什么我的getWidth返回0。
非常感谢任何帮助。
public void onActivityCreated(Bundle savedInstanceState)
中尝试了int paddingRightForTracker = buttonEditPoints.getWidth();
,但仍然得到0的宽度。FYI:buttonEditPoints =(ImageButton)theLayout.findViewById(R.id.buttonEditPoints);
在onCreateView中声明,因此它确实有一个赋值。 - mattdonders