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

发布于 2024-08-31 07:10:57 字数 2146 浏览 7 评论 0原文

我有以下主要布局:

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

这是我的视图示例:

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>

我正在尝试添加视图:

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

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

I have following main layout:

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

Here is example of my view:

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>

I am trying to add views:

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

I dont have any idea what is wrong with my code. I decided to put all my views in one file, but I still want to know how to fix my initial code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦里°也失望 2024-09-07 07:10:57

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

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

实现此目的的另一种方法是使用 包含合并标签,或 ViewStub。根据您的要求,这些可能不是一个选项,但您所描述的它们应该是,并且您应该使用这些而不是以编程方式执行,因为这样更干净。

Yout View viewUrl = (View) findViewById(R.layout.view_url); is very wrong. findViewById is kinda like the get(String key) method the the directory where your directory is your current view/activity. It only looks up the element with that Id under it's children.

To create Java objects out of XML files you need use you need to use the LayoutInflater. Which is pretty straight forward, out of that you get the object you can pass to viewstack.addView(..) method.

Another way to achieve this would be to just include the other XML files into the first one by using either the include, merge tags, or the ViewStub. Depending on your requirements these might not be an option though, but what you are describing they should be, and you should use these instead of doing it programatically because it's just cleaner this way.

薄荷港 2024-09-07 07:10:57

也许这有帮助:

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

maybe this help:

    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);
余生共白头 2024-09-07 07:10:57

它的名字表明它会通过 id 而不是通过布局来查找。您应该了解布局和 id 之间的区别。像这样使用 View v=(View)findViewById(R.id.your_view_id);

It's name suggests that It will find by id not by layout.You should understand difference between layout and id.use like this one View v=(View)findViewById(R.id.your_view_id);

坏尐絯℡ 2024-09-07 07:10:57

通过膨胀视图来添加的最简单方法。这里有一些小片段,您可以在其中找到参考。

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

Easiest way to add by inflating View. Here some small snippet where you can find reference.

 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);
    }
}
眼眸里的那抹悲凉 2024-09-07 07:10:57
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();

    }

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

    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文