【问题标题】:Adding a group of elements and making them appear dynamically when pressing a button in Android在Android中按下按钮时添加一组元素并使它们动态显示
【发布时间】:2013-03-23 01:37:44
【问题描述】:

我想添加一组元素(文本框、按钮、进度条和文本视图),并让它们在 Android 中按下按钮时动态显示。在每次按下按钮时,我都想创建一组元素并将它们放在相对布局中。这是我的oncreate 方法:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // The add button to insert new downloading file
    add = (Button) findViewById(R.id.button_add_url);
    add.setOnClickListener(this);

    // Swapping between pause and resume
    pause = (Button) findViewById(R.id.button_pause_resume);
    pause.setOnClickListener(this);

    download = (Button) findViewById(R.id.button_download);
    download.setOnClickListener(this);

    // The progress is written here
    text = (TextView) findViewById(R.id.textView2);
    text.setTextColor(Color.WHITE);

    progress = (ProgressBar) findViewById(R.id.progressBar1);
}

【问题讨论】:

    标签: android


    【解决方案1】:

    如果您将相对布局创建为包含这些元素的 xml,然后在单击按钮时您可以像这样(在您的按钮 onClick 方法上)将该布局膨胀到主布局,也许会更容易:

    LinearLayout mainLayout = (LinearLayout)findViewById(R.id.personal_data_root);
    RelativeLayout addLayout = (RelativeLayout)View.inflate(this, R.layout.layoutcontainingelementsouwanttoaddonclick, null);
    mainLayout.addView(addLayout);
    

    【讨论】:

    • 太好了,它终于奏效了,但出现在同一个地方,我应该如何将它们相对于另一个或换句话说底部最新的出现?
    • 我想你已经解决了这个问题,但如果没有,请查看这些问题stackoverflow.com/questions/5280844/…stackoverflow.com/questions/14139214/….. 基本上你需要的是 relativelayout.layoutparams,这样你就可以设置一些关于对齐的规则。跨度>
    猜你喜欢
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    • 2018-05-21
    • 2020-12-18
    • 1970-01-01
    • 2012-06-27
    • 2011-06-30
    • 1970-01-01
    相关资源
    最近更新 更多