【问题标题】:Display two TextViews from a Layout从一个布局中显示两个 TextView
【发布时间】:2014-02-07 16:43:35
【问题描述】:

在 Xamarin 中,如何垂直显示两个对象(在本例中为 TextViews)引用相同的 Resource.Id?

这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:id="@+id/TextViewAutoLink"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:autoLink="all">
</TextView>
</LinearLayout>

这是我的 C# 代码:

SetContentView (Resource.Layout.AutoLinkTextView);
TextView Web = (TextView)FindViewById(Resource.Id.TextViewAutoLink);
Web.Text = "Test address of http://www.google.com";

TextView Web2 = (TextView)FindViewById(Resource.Id.TextViewAutoLink);
Web2.Text = "Test address of http://www.stackoverflow.com";

Web2 TextView 是唯一正在显示的 TextView。

我可以帮忙吗?

提前致谢

【问题讨论】:

  • 为什么不在布局中添加另一个TextView

标签: android layout textview xamarin android-resources


【解决方案1】:

你可以在你的布局中添加两个 TextView。

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:id="@+id/TextViewAutoLink1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:autoLink="all">
</TextView>
<TextView
    android:id="@+id/TextViewAutoLink2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:autoLink="all">
</TextView>
</LinearLayout>

源代码

SetContentView (Resource.Layout.AutoLinkTextView);
TextView Web = (TextView)FindViewById(Resource.Id.TextViewAutoLink1);
Web.Text = "Test address of http://www.google.com";

TextView Web2 = (TextView)FindViewById(Resource.Id.TextViewAutoLink2);
Web2.Text = "Test address of http://www.stackoverflow.com";

【讨论】:

  • 如果我在一个布局中添加了两个 TextView,并且我不想使用其中一个 TextView,如何在不影响布局的情况下不显示其中一个 TextView?
  • @user22707 你可以在你的textViewsetText 不使用空(“”)或者你可以改变它的可见性
  • 您可以设置空文本 setText(""),您可以更改可见性 setVisibility(View.Gone) 或者您可以从布局中删除视图 yourLinearLayout.removeChildAt(0)
【解决方案2】:

对于两个不同的 TextView,您会得到相同的 TextView id。 所以当你设置你的文本时,它会覆盖以前的文本。

所以基本上你需要两个不同的TextView

【讨论】:

    【解决方案3】:

    您可以在两个文本之间使用“\n”。喜欢:

    SetContentView (Resource.Layout.AutoLinkTextView);
    TextView Web = (TextView)FindViewById(Resource.Id.TextViewAutoLink);
    Web.Text = "Test address of http://www.google.com"+"\n"+"Test address of  
    http://www.stackoverflow.com";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-28
      • 2011-12-16
      • 1970-01-01
      相关资源
      最近更新 更多