4

这个问题似乎已经被问了十几次了,但我无法从他们那里推断出解决方案。

我有一个名为的布局tile.xml,如下所示:

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:layout_marginTop="10dp"
    android:text="May Fong Robinson"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textStyle="bold" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="50dp"
    android:layout_marginTop="10dp"
    android:text="Beautiful star-shaped spillway, Kechut Reservoir, Jermuk, Armenia"
    android:textAppearance="?android:attr/textAppearanceMedium" />

我需要添加feed.xml我已声明为的视图之一,ScrollView因为我想在其中包含多个对象,以便可以垂直滚动它们。这是我的feed.xml文件:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:background="#F1F1F1"
    android:layout_height="fill_parent" >

</ScrollView>

我想在动态更改文本的同时将我的“平铺”动态添加tile.xml到“提要”视图中。feed.xmlTextViews

我怎样才能做到这一点?谢谢


新的feed.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#F1F1F1" >

    <LinearLayout
        android:id="@+id/tiles"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </LinearLayout>

</ScrollView>

...和新的tile.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="10dp"
        android:text="May Fong Robinson"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="10dp"
        android:text="Beautiful star-shaped spillway, Kechut Reservoir, Jermuk, Armenia"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>
4

3 回答 3

4

首先,在布局中添加 2 个 TextView。

然后,在您的代码中,您要在其中动态添加 TextViews,您可以执行以下操作:

ScrollView scroll = (ScrollView) findViewById(R.id.scrollView1);
LayoutInflater inflater =  (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.tile, null);
scroll.addView(view);

要访问 TextViews,您必须执行以下操作:

TextView textView1 = (TextView) view.findViewById(R.id.textView1);
textView1.setText("");

还要确保在 XML 文件中为您的 TextViews 提供不同的 ID。目前它们都是 textView1

于 2012-08-14T18:41:16.997 回答
1

ScrollView 只能有 1 个子项,这意味着您的“平铺”布局的当前结构无法添加到 ScrollView。您需要添加另一个布局,比如说 LinearLayout 到您的 ScrollView,这样它将成为 ScrollView 的唯一直接子级。您可以在此 LinearLayout 中添加任意数量的子元素。这可以通过使用您可能在许多示例中遇到的 LayoutInflater 来完成。

于 2012-08-14T18:41:34.753 回答
0
**TRY THIS**
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#F1F1F1" >

<LinearLayout
    android:id="@+id/tiles"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

   <include layout="@layout/tile" />

</LinearLayout>

于 2016-04-09T02:46:07.540 回答