gpt4 book ai didi

android - 如何从子 Activity 更改父 Activity 布局

转载 作者:行者123 更新时间:2023-11-29 01:17:20 24 4
gpt4 key购买 nike

我有一个全屏显示 Google map 的 Activity 。

我有一个扩展 MapsActivity 的子 Activity 。

当我从 Parent 扩展 child 时,它会在整个屏幕上显示 Map

Child Activity 中,我需要在顶部添加 Map 并在底部添加一个 ListView ,这将在 Child Activity Layout 中实现。

我尝试这样做,但它给了我以下错误消息,

Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

public class PlacesListActivity extends GoogleMapsActivity{

View parentFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_google_map);

parentFragment= findViewById(R.id.g_map);

LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));

linearLayout.addView(parentFragment);

在上面的代码中,我没有使用ChildActivityLayout,但是如果我使用它,它并没有获取到Parent Activity的元素。

最佳答案

您可以使用以下方法更改父 View :

View viewToBeRemoved= view.findViewById(R.id.view_to_be_removed);
View parent = viewToBeRemoved.getParent();
if (parent != null) {
((ViewGroup) parent).removeView(viewToBeRemoved);
}
parent.addView(modifiedViewToBeAdded);

But it is not recommended approach.

推荐的方法是在 Activity 的一半使用 map fragment ,并在 Activity 的剩余部分显示列表。

请参阅此线程以获取 How to use MapFragment

关于android - 如何从子 Activity 更改父 Activity 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38786742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com