2010-05-03 44 views
13

Tôi đã theo bố cục chính:Làm thế nào để lập trình thêm xem trong ViewFlipper

<LinearLayout android:id="@+id/LinearLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical"> 

<ViewFlipper android:id="@+id/viewstack" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <!-- Here I want to add my views which are located in separated xml files. --> 

     </ViewFlipper> 

</LinearLayout> 

Dưới đây là ví dụ về quan điểm của tôi:

view_url.xml

<LinearLayout android:id="@+id/LinearLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:gravity="center"> 
<EditText android:text="@+id/EditText01" 
    android:id="@+id/EditText01" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/> 
<Button android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/btnGenerate" 
       android:text="Generate"/> 
</LinearLayout> 

view_text.xml

<LinearLayout android:id="@+id/LinearLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical"> 

<EditText android:text="@+id/EditText01" 
    android:id="@+id/EditText01" 
    android:layout_height="wrap_content" 
    android:contentDescription="Enter your text here" 
    android:layout_width="fill_parent" 
    android:height="200dp"/> 
</LinearLayout> 

Tôi đang cố gắng thêm chế độ xem:

viewstack = (ViewFlipper) findViewById(R.id.viewstack);)); 

View viewText = (View) findViewById(R.layout.view_text); 
viewstack.addView(viewText); < -- Emulator is crashing at this line 
View viewUrl = (View) findViewById(R.layout.view_url); 
viewstack.addView(viewUrl); 

Tôi không biết có gì sai với mã của mình. Tôi quyết định đưa tất cả các quan điểm của mình vào một tệp, nhưng tôi vẫn muốn biết cách sửa mã ban đầu của mình.

Trả lời

8

Yout View viewUrl = (View) findViewById(R.layout.view_url); rất sai. findViewById giống như phương thức get (String key) trong thư mục chứa thư mục/hoạt động hiện tại của bạn. Nó chỉ tìm kiếm các yếu tố với Id đó dưới con của nó.

Để tạo các đối tượng Java ra khỏi các tệp XML bạn cần sử dụng, bạn cần sử dụng LayoutInflater. Đó là khá thẳng về phía trước, trong số đó bạn nhận được đối tượng mà bạn có thể chuyển sang phương thức viewstack.addView (..).

Một cách khác để đạt được điều này là chỉ bao gồm các tệp XML khác vào tệp đầu tiên bằng cách sử dụng các thẻ include, merge hoặc ViewStub. Tùy thuộc vào yêu cầu của bạn, đây có thể không phải là một lựa chọn, nhưng những gì bạn đang mô tả họ nên, và bạn nên sử dụng chúng thay vì làm nó theo chương trình bởi vì nó chỉ sạch hơn theo cách này.

+0

Cảm ơn sự giúp đỡ của bạn. Tôi sẽ thử cả hai cách. – barmaleikin

0

Tên của nó cho thấy rằng Nó sẽ tìm theo id không theo bố cục.Bạn nên hiểu sự khác biệt giữa bố cục và id.use như thế này Xem v = (Xem) findViewById (R.id.your_view_id);

2

có thể trợ giúp này:

this.flipper=(ViewFlipper)findViewById(R.id.flipper); 

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    this.viewLoader=(View)inflater.inflate(R.layout.search_result_grid, null); 
    flipper.addView(viewLoader);   

    this.viewResultGrid=(View)inflater.inflate(R.layout.search_result_grid, null); 
    gvSearchResult=(GridView)viewResultGrid.findViewById(R.id.gridViewSearchResult);   
    flipper.addView(viewResultGrid); 
-1
use viewflipper 

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@drawable/white" 
     android:orientation="vertical" > 
    <ViewFlipper 
     android:id="@+id/view_flipper" 
     android:layout_width="match_parent" 
     android:layout_height="410dp" > 


       <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:gravity="center" 
       android:orientation="vertical" > 
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="15dp" 

        android:text="first layout" 
        android:textColor="#845965" 
        android:textSize="25dp" 
        android:textStyle="bold" > 
       </TextView> 


      </LinearLayout> 


       <LinearLayout 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:gravity="center" 
       android:orientation="vertical" > 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="15dp" 

        android:text="second layout" 
        android:textColor="#654123" 
        android:textSize="25dp" 
        android:textStyle="bold" > 
       </TextView> 


      </LinearLayout> 
    //you can add many layout here 
    </viewFlipper> 
     <Button 
        android:id="@+id/flipbyclickNext" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="@drawable/bn1" 
        android:onClick="flipByClickNext" /> 
<Button 
        android:id="@+id/flipbyclickprevious" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 

        android:onClick="flipByClickPrevious" 
        android:background="@drawable/bp1" /> 
    </LinearLayout> 

MainActivity.java

public class MainActivity extends Activity { 
    private ViewFlipper viewFlipper; 

    //Button next,prev; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     viewFlipper = (ViewFlipper) findViewById(R.id.view_flipper); 
} 
public void flipByClickNext(View v) 
    { 
     if(viewFlipper.isFlipping())//Checking flipper is flipping or not. 
     {  
      viewFlipper.stopFlipping();  //stops the flipping . 
     } 

     viewFlipper.showNext();//shows the next view element of ViewFlipper 
    } 

    public void flipByClickPrevious(View v) 
    { 
     if(viewFlipper.isFlipping())//Checking flipper is flipping or not. 
     {  
      viewFlipper.stopFlipping();  //stops the flipping . 
     } 
     viewFlipper.showPrevious(); 

    } 

} 
+0

Mã của bạn trả lời câu hỏi như thế nào? Anh ấy đang cố gắng thổi phồng một chế độ xem tùy chỉnh lên trình kéo. –

0

Cách dễ nhất để thêm bằng cách bơm View. Dưới đây là một số đoạn mã nhỏ nơi bạn có thể tìm thấy tài liệu tham khảo.

private View viewText; 
private TextView txtPost; 
private void setViewFlipperPost(String postData, String postType) { 

    if (postType.toLowerCase().toString().equals("text")) { 

      viewText = LayoutInflater.from(mContext).inflate(R.layout.activity_full_screen_textpost, null, false); 
      viewText.setTag(TAG_TEXT); 

      txtPost = (TextView) viewText.findViewById(R.id.txtTextPostFullScreenText); 
      txtPost.setText(postData); 

      viewFlipper.addView(viewText); 
    } 
} 
Các vấn đề liên quan