2012-01-23 77 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); 

    }