从布局中删除子项

问题描述 投票:2回答:1

在Xamarin中,如何获取当前布局以便可以从布局中删除子项?

这是我的代码:

SetContentView (Resource.Layout.AutoLinkTextView);

TextView Email = (TextView)FindViewById(Resource.Id.TextViewEmail);
Email.Text = "Test Email of [email protected]";

TextView Phone = (TextView)FindViewById(Resource.Id.TextViewPhone);
Phone.Text = "Test Phone of 0800 64 64 64";

TextView Web = (TextView)FindViewById(Resource.Id.TextViewWeb);
Web.Text = "Test Address of http://www.google.com" + "\n" + "Test Address of http://www.stackoverflow.com";

在我的AutoLinkTextView布局中,我有一个带有以下id的TextView,我希望从布局中删除它:

android:id="@+id/TextViewMap"

我能帮忙做一下吗?

提前致谢

编辑

我能为代码提供一些帮助吗?

这是我的代码:

LinearLayout layout = (LinearLayout)FindViewById (0);
View ViewToRemove = layout.GetChildAt (1);
layout.RemoveView (ViewToRemove);

使用上面的代码,根本没有显示任何TextView对象。

android android-layout layout textview xamarin
1个回答
3
投票

首先给Main布局一些id。喜欢:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/linearLayout">
//All ur textviews
</LinearLayout>

然后,在onCreate()中编写以下代码:

LinearLayout _linear = (LinearLayout) findViewById(Resource.id.linearLayout);
TextView textViewMap = (TextView) findViewById(Resource.id.TextViewMap); 
_linear.removeView(textViewMap);         
© www.soinside.com 2019 - 2024. All rights reserved.