2012-01-23 78 views
1

我使用下面的代码到多个TextViews添加到CommonsWare ViewSwiper但我看到重叠TextViews:添加多个视图ViewSwiper动态

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" /> 

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

回答

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); 

    }