【发布时间】:2015-09-11 12:05:46
【问题描述】:
我在一个具有相对布局的 Activity 中有 5 个按钮。我有一个名为 init() 将使用 setX() 和 setY() 重新定位按钮。当我从 onClickListener 内部调用 init() 时,按钮会重新排列,没有任何问题。但是当我从 onCreate() 或 onStart() 调用该函数时,日志显示该函数已执行,但按钮保持在同一位置。我该怎么办?
另外,如果我从 onResume() 调用 init(),按钮的重新定位不会出现问题。
public class MainActivity extends ActionBarActivity {
@Override
protected void onStart()
{
super.onStart();
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//if init() is called here I can see the Log "Tag/Init Executed" but the buttons are not repositioned
}
@Override
protected void onResume()
{
Log.i("Log", "resume called");
super.onResume();
//if i call init() here, the button is not repositioned but if i click home button and resume the app again, the button is repositioned.
}
public void init()
{
Log.i("Tag","Init Executed");
b1=(ImageButton)findViewById(R.id.imageButton);
b1.setX(p.x);
b1.setY(p.y);
}
}
【问题讨论】:
-
提供一些代码....我认为您在视图层次结构完全膨胀之前就这样做了
-
我第二,但在
onStart()中,视图层次结构应该已经膨胀,一点代码会有所帮助
标签: android android-activity dynamic android-view