动态地向ViewSwiper添加多个视图

时间:2012-01-23 09:14:37

标签: android commonsware-cwac

我使用以下代码将多个TextView添加到CommonsWare ViewSwiper,但我看到重叠的TextViews:

    for (int i = 0; i < 10; i++){

        LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = vi.inflate(R.layout.c_layout, null);

        // fill in any details dynamically here
        TextView textView = (TextView) v.findViewById(R.id.myTextView);
        textView.setText("This is text " + i);

        swiper.addView(textView);

    }

以下是c_layout.xml的布局:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

这样做的正确方法应该是什么。

1 个答案:

答案 0 :(得分:0)

使用以下代码使其工作,以防其他人需要帮助:

    ViewSwiper swiper = (ViewSwiper)findViewById(R.id.swiper); 

    ViewFlipper flipper = swiper.getFlipper();

    for (int i = 0; i < 10; i++){

        LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = vi.inflate(R.layout.trivia_text, null);

        // fill in any details dynamically here
        TextView textView = (TextView) v.findViewById(R.id.triviaText);
        textView.setId(i);
        textView.setText("This is text " + i);

        flipper.addView(textView);

    }