动态添加按钮到已存在的按钮阵列
问题描述:
我试图为每一年开始使用我的应用程序以来创建一个按钮。所以我的XML文档中我有动态添加按钮到已存在的按钮阵列
<HorizontalScrollView
android:id="@+id/yearScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:layout_gravity="center">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/currentYear"
android:tag="01"
android:text="2015"
android:paddingLeft="8.0dip"
android:paddingRight="8.0dip"
android:height="24dp"
android:textSize="18sp"
android:textColor="#333333"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
>
</Button>
</LinearLayout>
</HorizontalScrollView>
然后,我有以下代码
private List<Button> yearButtons;
private static final int[] YEAR_BUTTON_IDS = {
R.id.currentYear,
};
然后,我必须找出它是哪一年,并覆盖我的当前按钮
int firstYear = Integer.parseInt(year);
yearButtons.get(0).setText(String.valueOf(CurrentYear));
然后在我的init类我证实了按钮,我知道我不需要一个循环只有一个按钮,但留下这样就与月份按钮的工作方式一致
for(int id : YEAR_BUTTON_IDS) {
Button button = (Button)findViewById(id);
button.setOnClickListener(this);
yearButtons.add(button);
}
然后,我有一些逻辑来找出第一年,他们就开始叫yearsOfTransactions
然后,我有以下的循环,我试图创建按钮
for (int i =0; i< yearsOfTransactions; i++){
int yearToAdd = CurrentYear-i-1;
Button myButton = new Button(this);
myButton.setText(String.valueOf(yearToAdd));
yearButtons.add(myButton);
}
但是这是行不通的。
感谢您的帮助
答
我在你的代码进行少许修改:
<HorizontalScrollView
android:id="@+id/yearScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:layout_gravity="center">
<LinearLayout
android:id="@+id/button_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/currentYear"
android:tag="01"
android:text="2015"
android:paddingLeft="8.0dip"
android:paddingRight="8.0dip"
android:height="24dp"
android:textSize="18sp"
android:textColor="#333333"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
>
</Button>
</LinearLayout>
</HorizontalScrollView>
现在做的年数组中添加(中的onCreate())
int[] yearToAdd = {2000, 2001, 2002, 2003};
LinearLayout parentLayout = (LinearLayout)findViewById(R.id.button_parent);
for (int i =0; i< yearToAdd.lenght; i++){
int yearToAdd = yearToAdd[i];
Button myButton = new Button(this);
myButton.setText(String.valueOf(yearToAdd));
yearButtons.add(myButton);
yearButtons.setOnClickListener(this);
parentLayout.addView(myButton);
}
让我知道的情况下,希望它有助于:)
答
您必须将按钮添加到线性布局。 这是我用来在线性布局中添加动态按钮的功能。
public Button createButton(String label) {
Button button = new Button(mContext);
button.setText(label);
mDynamicLayout.addView(button, mLayoutParam);
return button;
}
答
您应该将您创建的每个按钮添加到LinearLayout。首先将xml中的id和id设置为LinearLayout。在你的课堂上找到视图,最后添加按钮。
LinearLayout container = findViewById(R.id.container);
//...
for (int i =0; i< yearsOfTransactions; i++){
int yearToAdd = CurrentYear-i-1;
Button myButton = new Button(this);
myButton.setText(String.valueOf(yearToAdd));
yearButtons.add(myButton);
// you missed that
container.addView(myButton);
}
答
please try this inside your for loop
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);
//set the properties for button
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setText("Button");
btnTag.setId(some_random_id);
//add button to the layout
layout.addView(btnTag);
不工作的手段?你需要添加它在你的线性布局..然后它会变得可见..! –
我建议你不要把'int []'作为'Arraylist'的id。因为在创建一次后你不能修改数组大小.. !! –