【发布时间】: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(这会创建新作业)。
使用此代码,我所能做的就是创建文本视图和按钮(如下所示),但是我需要它来创建作业按钮下方的按钮和文本视图。
如果您无法回答这个问题,请提供建议,让我更好地提问(我已阅读规则)
【问题讨论】:
-
或许可以看看ExpandableListView是如何实现的developer.android.com/reference/android/widget/…
-
为什么不使用 ExpandableListView。这对你来说更好,你的观点会被回收。
-
天哪,谢谢大家。我讨厌答案如此简单:P。我现在就试一试
-
嘿伙计们,我无法使用 ExpandableListView 做到这一点,但我更接近了,这就是我现在想做的事情stackoverflow.com/questions/35101639/…