如何以编程方式在ViewFlipper添加视图 [英] How to programmatically add view in ViewFlipper

查看:137
本文介绍了如何以编程方式在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>

下面的例子是我的观点:

Here is example of my view:

view_url.xml

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

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

我没有任何想法有什么不对我的code。我决定把我所有的观点在一个文件中,但我还是想知道如何解决我的初始code。

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.

推荐答案

YOUT 查看viewUrl =(查看)findViewById(R.layout.view_url); 是非常错误的。 findViewById是有点像获取(字符串键)方法,你的目录是当前视图/活动目录。它不仅外观与该ID的元素在它之下的孩子。

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.

要创建Java对象,你需要用你需要使用 LayoutInflater 的XML文件了。这是pretty的直线前进,那出你的对象,你可以传递给viewstack.addView(..)方法。

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.

另一种方式来实现,这将是只包含其他XML文件到第一个通过使用了的包括的,合并的标签,或的ViewStub 。根据您的要求,这些可能不是一种选择,虽然,但你所描述,他们应该是,你应该用做编程的这些,而不是因为它只是更清洁的这样的东西。

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.

这篇关于如何以编程方式在ViewFlipper添加视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆