ssummeraw 2013-07-08 09:30 采纳率: 10%
浏览 4056
已采纳

使用 layout inflatter 如何在另一个 view 中添加 view?

我使用两个 buttons 创建了 mainLayout。这两个按钮是 add 和 remove。
add: 添加另外的 layout

remove : 移除其它的 layout

 <Button
             android:id="@+id/btnAdd"
             android:textStyle="bold"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_gravity="center_horizontal"
             android:text="Add View"
             android:onClick="addView" />
      <Button
             android:id="@+id/btnRemove"
             android:textStyle="bold"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:layout_gravity="center_horizontal"
             android:text="Remove View"
             android:onClick="removeView" />

下面的代码实现的是当我点击 addView 按钮时添加 view:

LayoutInflater inflater= (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
    view=inflater.inflate(R.layout.other_layout,null);
    mainLayout.addView(view);

这个 view 是添加在 main layout下面的。我想把 view 添加在 addView 按钮的右下边,removeView 按钮的上面,但是不在 main Layout 的底部。
如何实现?

  • 写回答

2条回答 默认 最新

  • xiaoyan_12 2013-07-09 08:28
    关注

    在两个按钮之间添加一个framelayout。
    然后在执行时把 view放到 framelayout中。

    LayoutInflater inflater= (LayoutInflater)this.getSystemService (LAYOUT_INFLATER_SERVICE);    
    view=inflater.inflate(R.layout.other_layout,null);  
    myframeLayout.addView(view); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题