2016-06-14 389 views
0

我試圖爲每一年開始使用我的應用程序以來創建一個按鈕。所以我的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); 

    } 

但是這是行不通的。

感謝您的幫助

+0

不工作的手段?你需要添加它在你的線性佈局..然後它會變得可見..! –

+0

我建議你不要把'int []'作爲'Arraylist '的id。因爲在創建一次後你不能修改數組大小.. !! –

回答

1

我在你的代碼進行少許修改:

<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); 
} 

讓我知道的情況下,希望它有助於:)

0

您必須將按鈕添加到線性佈局。 這是我用來在線性佈局中添加動態按鈕的功能。

public Button createButton(String label) { 
    Button button = new Button(mContext); 
    button.setText(label); 
    mDynamicLayout.addView(button, mLayoutParam); 
    return button; 
} 
0

您應該將您創建的每個按鈕添加到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); 
} 
0
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);