代码之家  ›  专栏  ›  技术社区  ›  barmaleikin

如何在ViewFlipper中以编程方式添加视图

  •  13
  • barmaleikin  · 技术社区  · 16 年前

    我有以下主要布局:

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

    以下是我的观点示例:

    查看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>
    

    查看文本.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>
    

    我正在尝试添加视图:

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

    我不知道我的代码有什么问题。我决定把所有的视图放在一个文件中,但我仍然想知道如何修复我的初始代码。

    5 回复  |  直到 15 年前
        1
  •  8
  •   Mart Roosmaa    16 年前

    View viewUrl = (View) findViewById(R.layout.view_url); 是非常错误的。findViewById有点像get(String key)方法目录是当前视图/活动所在的目录。它只在子元素下查找具有该Id的元素。

    要从需要使用的XML文件中创建Java对象,需要使用 LayoutInflater . 这是非常直接的,从中可以得到可以传递给viewstack.addView(..)方法的对象。

    另一种方法是通过使用 include , merge 标签,或 ViewStub . 根据您的需求,这些可能不是一个选项,但是您所描述的应该是什么,您应该使用这些,而不是以编程的方式执行,因为这样做更干净。

        2
  •  3
  •   Mirodil    14 年前

    也许这有帮助:

        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);
    
        3
  •  0
  •   user766090user766090    15 年前

    它的名字表明它是按id而不是按布局来查找的。你应该了解布局和id之间的区别。像这样使用一个视图v=(View)findViewById(R.id.your_View_id);

        4
  •  0
  •   Kuls    10 年前

    通过放大视图添加的最简单方法。这里有一些小片段,你可以在这里找到参考。

     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);
        }
    }
    
        5
  •  -1
  •   user2975575    12 年前
    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>
    

    main活动.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();
    
        }
    
    }