2011-11-02 97 views
0

这里是我的xml如何动态添加布局?

<TextView android:id="@+id/itemNameTextView" 
    android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:textColor="#000000" android:textSize="30sp" 
    android:layout_centerVertical="true" android:drawableLeft="@drawable/doller" 
    android:drawablePadding="30dp" android:layout_marginLeft="30dp"/> 

<Button android:id="@+id/buttonAdd" 
    android:layout_width="wrap_content" android:layout_height="fill_parent" 
    android:textColor="#000000" android:background="#00000000" 
    android:layout_alignParentRight="true" android:layout_marginRight="10dp" 
    android:text="ADD" android:textSize="25sp" android:textStyle="bold" /> 

<EditText android:id="@+id/displayPriceEditText" 
    android:layout_width="wrap_content" android:layout_height="wrap_content" 
android:textColor="#000000" android:textSize="30sp" android:hint="0.00" 
    android:layout_centerVertical="true" android:layout_toLeftOf="@id/buttonAdd" 
    android:layout_marginRight="40dp" android:background="#00000000"/> 

我应该能够添加一些文字都在EDITTEXT和TextView的,当我按一下按钮相同的布局已出现略低于此一个(如列表)。任何人都可以帮助我吗?

回答

0

有两个XML文件...行(row.xml)或什么都重复使用的东西,你要添加为一个视图是一个和它需要被添加到视图是另一个listbased.xml ...在第二个有一个按钮和所有其他的东西需要进去....确保你有一个父视图内listbased.xml(相对,线性,列表视图)。 ...或者类似的东西...

现在

...在...类,其中这个方法需要被调用设置R.setContentView(r.layout.listbased)....

您实现一键收听然后添加吹气(ex:parentLayout.addView(mInflater.inflate(R.layout.row)))...其中

public LayoutInflater mInflater;被定义为一个全球性的...

+0

耶得到它。谢谢 :) – harish

0

您必须在runtime.and创建相似的布局,然后您可以将整个布局添加到此视图。因此,你必须在你的代码中有一个LinearLayout/RelativeLayout对象,你也必须以编程方式创建EditText,Button和TextView,根据需要设置它们的属性,然后将所有的组件添加到你的LinearLayout/RelativeLayout对象。

然后,您可以将该对象添加到您的主布局。

是的,你需要有你的XML文件的布局对象,然后只有,你可以添加上面的对象。

-1

父应该是linearLayout。 再加入由

LinearLayout parentLayout = (LinearLayout) findViewById(id); 

parentLayout.addView(childView)