我试图在运行时将 TextViews 添加到包裹在 ScrollView 中的 LinearLayout,但是 TextViews 的可见性将最终 不会显示在屏幕上,即使它们的可见性标志都设置为
Visible
.
myLayout = FindViewById<LinearLayout>(Resource.Id.myScollLayout);
View myView = LayoutInflater.From(this).Inflate(Resource.Layout.my_view, null);
TextView text1 = myView.FindViewById<TextView>(Resource.Id.text1);
TextView text2 = myView.FindViewById<TextView>(Resource.Id.text2);
TextView text3 = myView.FindViewById<TextView>(Resource.Id.text3);
text1.Text = "text 1";
text2.Text = "text 2";
text3.Text = "text 3";
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
myLayout.AddView(myView, p);
myView.Visibility = ViewStates.Visible
xml布局
<ScrollView
android:layout_width="match_parent"
android:padding="8dp"
android:layout_marginBottom="16dp"
android:id="@+id/myScrollView"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:visibility="visible"
android:id="@+id/myScrollLayout"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
我说最终是因为有时上面代码中的视图会按预期显示,但最终在后续执行膨胀和添加视图的代码时,添加到膨胀布局的视图在屏幕上不可见。如果我检查所有子视图的可见性,它们都说
Visible
,但在屏幕上它们似乎显示为Invisible
,因为滚动布局内仍有空间占用,但为空白。
我参考了许多其他讨论使用以下内容的文章:
但以上的任何组合似乎都无法使视图出现。我还在下面实现了一个功能,试图强制
myScrollLayout
的所有子视图都可见,但仍然没有任何影响,因为所有视图看起来都好像是 Invisible
.
void SetVisibility(View v)
{
if (v.GetType() == typeof(LinearLayout))
{
var view = (LinearLayout)v;
if (view.ChildCount == 0)
{
((Activity)view.Context).RunOnUiThread(() => view.Visibility = ViewStates.Visible);
return;
}
else
{
((Activity)view.Context).RunOnUiThread(() => view.Visibility = ViewStates.Visible);
for (int i = 0; i < view.ChildCount; i++)
{
SetVisibility(view.GetChildAt(i));
}
}
}
else
{
((Activity)v.Context).RunOnUiThread(() => v.Visibility = ViewStates.Visible);
return;
}
}
所以一般来说,即使 View 及其父视图的可见性都设置为
Visible
,什么会导致 View 不显示在屏幕上?
**更新:** 上面代码中
myLayout
的目的是保存可变数量的TextView,这些TextView将根据需要显示(Visible
)或隐藏(Invisible
)。当需要显示新的 TextView 时,使用 View.RemoveAllViews
删除之前的 TextView。
我无法重现你的问题。我在一个新的空白 xamarin.android 项目中测试了您的代码,textview 将显示。
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:padding="8dp"
android:layout_marginBottom="16dp"
android:id="@+id/myScrollView"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:visibility="visible"
android:id="@+id/myScollLayout"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
my_view.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/text3"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
代码将文本添加到文本视图:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
var myLayout = FindViewById<LinearLayout>(Resource.Id.myScollLayout);
View myView = LayoutInflater.From(this).Inflate(Resource.Layout.my_view, null);
TextView text1 = myView.FindViewById<TextView>(Resource.Id.text1);
TextView text2 = myView.FindViewById<TextView>(Resource.Id.text2);
TextView text3 = myView.FindViewById<TextView>(Resource.Id.text3);
text1.Text = "text 1";
text2.Text = "text 2";
text3.Text = "text 3";
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
myLayout.AddView(myView, p);
myView.Visibility = ViewStates.Visible;
}
结果图:
另外,我也尝试过把textview中添加文本的代码放到一个按钮的点击事件中。我得到了相同的结果。