如何在 Activity 之上覆盖视图?
有没有办法在当前活动的视图之上动态添加视图?需要注意的一件事是,这需要从另一个只能访问活动上下文的类来完成。
例如:
public class ActivityClass extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
…
PopupClass popup = new PopupClass(this);
popup.showPopup();
}
}
public class PopupClass
{
Context context;
void Popup(Context ctx)
{
context = ctx;
}
void showPopup()
{
// Add a view on top of the current Activity.
}
}
有没有办法让 PopupClass 能够在仅了解上下文的情况下向当前 Activity 添加视图?
PopupClass 不知道有关 Activity 的任何其他信息,也无法将 View 传递回 ActivityClass 供 ActivityClass 添加。
Is there a way to add a View dynamically on top of the current Activity's view? One thing to note is this needs to be done from another class which only has access to the Activity's context.
ex:
public class ActivityClass extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
…
PopupClass popup = new PopupClass(this);
popup.showPopup();
}
}
public class PopupClass
{
Context context;
void Popup(Context ctx)
{
context = ctx;
}
void showPopup()
{
// Add a view on top of the current Activity.
}
}
Is there a way for PopupClass to be able to add a view to the current Activity with just knowing the context?
PopupClass does not know anything else about the Activity nor can it pass back the View to ActivityClass for ActivityClass to add.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我为此使用自定义对话框。
http://www.androidpeople.com/android-custom-dialog-example
http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application
I use custom Dialog's for this.
http://www.androidpeople.com/android-custom-dialog-example
http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application
我为此目的使用相对布局。
我想你也可以使用框架布局。
看看这里:
http://www.learn-android.com/ 2010/01/05/android-layout-tutorial/5/
I'm using relative layouts for that purpose.
I guess you can also use frame layouts.
Take a look here:
http://www.learn-android.com/2010/01/05/android-layout-tutorial/5/
为什么不让 PopClass 扩展 Activity 并在 PopupClass 活动的 AndroidManifest.xml 属性中设置
android:theme="@android:style/Theme.Dialog"
然后在 ActivityClass 中,而不是使用中的代码您展示的示例,您可以使用...
当 PopupClass 活动完成时,它可以在另一个 Intent 中设置返回数据并完成。
然后在 ActivityClass 中重写 onActivityResult(...) 并处理返回的 Intent。
Why not make PopClass extend Activity and in the AndroidManifest.xml attributes for the PopupClass activity, set
android:theme="@android:style/Theme.Dialog"
Then in ActivityClass, instead of using the code in the sample you showed, you could use...
When the PopupClass activity is finished it can set return data in another Intent and finish.
Then in the ActivityClass you override
onActivityResult(...)
and process the returned Intent.