我试图为每一年开始使用我的应用程序以来创建一个按钮。所以我的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);
}
但是这是行不通的。
感谢您的帮助
不工作的手段?你需要添加它在你的线性布局..然后它会变得可见..! –
我建议你不要把'int []'作为'Arraylist'的id。因为在创建一次后你不能修改数组大小.. !! –