【问题标题】:Creating a new linear layout with specific paramaters inside another linear layout在另一个线性布局中创建具有特定参数的新线性布局
【发布时间】:2016-01-27 14:05:50
【问题描述】:

我正在尝试创建一个切换按钮,它将显示更多按钮和一些 TextView,如下所示。

当点击切换按钮时,它会显示 3 个文本视图并排显示 3 个彼此相邻的按钮。一旦关闭线性布局 2(特定于该作业编号)将被隐藏,其他按钮相应地向上移动。

这就是我所拥有的

    public void createNewJob(String JobNo) {
    LinearLayout layout;
    layout = (LinearLayout) findViewById(R.id.ReportsLayout);
    ToggleButton NewJob = new ToggleButton(this);
    NewJob.setText(JobNo);
    NewJob.setTextSize(setDP(40f));
    layout.addView(NewJob);
    //
    //int JobNoInt = new Integer(Integer.parseInt(JobNo));
    //NewJob.setId(JobNoInt);
    NewJob.setOnClickListener(openToggle(NewJob, this));
}

public View.OnClickListener openToggle(final ToggleButton button, final Context context){
    return new View.OnClickListener() {
        public void onClick(View v) {
           // LinearLayout layout = new LinearLayout(Reports.this);
            LinearLayout layout = (LinearLayout) findViewById(R.id.ReportsLayout);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
            //layout.setLayoutParams(lp);
            //layout.setLayoutParams(lp);
            //layout.setOrientation(LinearLayout.HORIZONTAL);
            TextView viewMachine = new TextView(Reports.this);
            TextView viewItem = new TextView(Reports.this);
            TextView viewDate = new TextView(Reports.this);
            Button enterJob = new Button(Reports.this);
            Button editJob = new Button(Reports.this);
            Button exportJob = new Button(Reports.this);
            //enterJob.setLayoutParams(lp);
            //editJob.setLayoutParams(lp);
            //exportJob.setLayoutParams(lp);
           if( button.isChecked()==(true)) {
               layout.addView(viewMachine,lp);
               layout.addView(viewItem,lp);
               layout.addView(viewDate,lp);

               layout.addView(enterJob,lp);
               layout.addView(editJob, lp);
               layout.addView(exportJob, lp);
           }
            else{
                layout.setVisibility(LinearLayout.GONE);
            }
        }
    };
}

每当打开报告 xml 时都会运行 createNewJob(这会创建新作业)。

使用此代码,我所能做的就是创建文本视图和按钮(如下所示),但是我需要它来创建作业按钮下方的按钮和文本视图。

如果您无法回答这个问题,请提供建议,让我更好地提问(我已阅读规则)

【问题讨论】:

标签: android android-layout


【解决方案1】:

@Jube 你需要设置 LinearLayout 2 android:visibility="gone" 当点击 Toggle 按钮设置 LinearLayout 2 可见性 Visible by layout2.setVisibility(View.VISIBLE); in Java。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多