weixin_42446395 2011-04-21 16:24
浏览 294
已采纳

Android java 布局

xml文件
RelativeLayout_A.xml
代码
RelativeLayout_A.xml

........

........


........

........

RelativeLayout_A1.xml 去除C的布局
RelativeLayout_C1.xml C1的布局
RelativeLayout_C2.xml C2的布局
其中RelativeLayout C的布局会变化,但是其它布局不变
我想在Java中动态的添加会变的C,比如添加C1,删除C1,添加C2等等
怎么实现啊,以前都是xml布局,不会java布局
详细一点,先谢谢大家了

  • 写回答

1条回答 默认 最新

  • iteye_17793 2011-04-22 11:40
    关注

    可以使用ViewStub
    大约参考代码如下:

    main.xml
    [code="xml"]<?xml version="1.0" encoding="utf-8"?>

    android:layout_width="fill_parent" android:layout_height="fill_parent">
    android:layout_height="wrap_content" android:text="Showing ViewStub" />

    android:layout_height="wrap_content" android:text="Open ViewStub" />

    android:layout="@layout/layout1" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:layout_gravity="bottom"/>

    [/code]

    layout1.xml

    [code="xml"]<?xml version="1.0" encoding="UTF-8"?>
    android:layout_width="fill_parent" android:layout_height="wrap_content">
    android:layout_height="wrap_content" android:text="Textview from Viewstub" />

    android:layout_height="wrap_content" android:minWidth="100dip"
    android:text="Next" />

    [/code]

    SampleViewStub.java

    [code="java"]public class SampleViewStub extends Activity {

    ViewStub stub;
    boolean click = true;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    findViewById(R.id.openstub).setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
    if (click) {
    stub = (ViewStub) findViewById(R.id.stub1);
    stub.inflate();
    click = false;
    }
    }
    });
    }
    }[/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功