2013-04-30 65 views
1

所以,我試圖製作一列按鈕。在搜索解決方案後,我能找到的最好的方法是將其放置到LinearLayout或類似的東西中,但我的問題是我使用的是片段(特別是導航類型「Tabs + Swipe」),並且它不使用盡我所能看到的佈局。 (嗯,好吧,是這樣,但不是在pageviewer)爲了得到一個按鈕我修改生成的代碼看起來像在片段的佈局中製作一列按鈕

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false); 
     final TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label); 

     final Button button = (Button) rootView.findViewById(R.id.button1); 
     button.setText("Btn Text"); 
     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
            // Perform action on click 
      } 
     }); 

了在reddit的/ LearnProgramming諮詢的球員後,我反而告知製造的每個按鈕的XML,我應該編程按鈕和更改佈局參數。我有點困惑,失去了他們的幫助,所以我回到了谷歌。 在那之後,我的代碼現在看起來像:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false); 
     TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label); 
     dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER))); 

     for (int i=0;i<4;i++){ 
      final Button newButton = new Button(getActivity()); 
      newButton.setText("This is some text for"+i); 
      newButton.setTag(i); 
      arrList.add(newButton); 
      // ((ViewGroup) rootView).addView(newButton); // Works for a single button 

      ((ViewGroup) rootView).addView(newButton, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 

      newButton.setOnClickListener(new View.OnClickListener() { 
       public void onClick(View v) { 
        newButton.setText("I changed it."); 
       } 
      }); 
      } 

     return rootView; 
    } 

這是什麼導致了一個默認的TextView的數量,說明我什麼網頁和重疊的四個按鈕。我知道它重疊,並且它們都被顯示,因爲我可以看到數字,重疊1-4。我對編程並不陌生,但我對android編程還很陌生。

我想要發生的是將按鈕添加到彼此的下面。 任何人都可以把我推向正確的方向嗎?我儘可能地做到了這一點,但如果您需要更多信息,我會很樂意添加它。 在此先感謝。

編輯: 這裏的fragment_main_dummy.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="vertical" 
    tools:context=".MainActivity$DummySectionFragment" > 

    <TextView 
     android:id="@+id/section_label" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <LinearLayout 
     android:id="@+id/MyLayout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/section_label" 
     android:layout_marginTop="16dp" 
     android:layout_toRightOf="@+id/section_label" 
     android:orientation="vertical" > 
    </LinearLayout> 

</RelativeLayout> 

所以這是相對的,但我沒有看到一個參考代碼的任何地方RelativeLayout的,所以我帶領相信,這是不正在使用? 此外,我嘗試添加一個LinearLayout作爲XML中的一個孩子,但我沒有做任何事情。

+0

沒有人可以幫助你,如果你不提什麼佈局,其中的'正在增加Buttons'。如果'rootView'是一個'RelativeLayout'或'FrameLayout',這是正常的按鈕重疊,因爲你沒有特殊的'LayoutParams'。 – Luksprog 2013-04-30 17:14:37

+0

我不認爲有一個。這只是默認eclipse項目中帶有「Tabs + Swipe」的代碼。 – Vasir 2013-04-30 17:46:26

+0

那麼,你可以看看看看'fragment_main_dummy.xml'文件是怎麼樣的?根標記是一個「FrameLayout」或「RelativeLayout」(或者你可以在問題中發佈它) – Luksprog 2013-04-30 17:49:00

回答

1

試試這個:

for (int i=0;i<4;i++){ 
    final Button newButton = new Button(getActivity()); 
    newButton.setText("This is some text for"+i); 
    newButton.setTag(i); 
    arrList.add(newButton); 
    newButton.setId(1000 + i); 
    RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    rlp.addRule(RelativeLayout.BELOW, i == 0 ? R.id.MyLayout : newButton.getId() - 1); 
    newButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      newButton.setText("I changed it."); 
     } 
    }); 
    ((RelativeLayout) rootView).addView(newButton, rlp); 
} 
+0

這太棒了。有用。謝謝。 所以我沒有做錯任何事,我從來沒有給它一個規則來定位它在以前的觀點下? 我有標籤來跟蹤我點擊了哪些內容,現在我應該可以使用標籤或ID來跟蹤我在數組列表中按哪個按鈕,對嗎? – Vasir 2013-04-30 18:36:54

+0

@Vasir是'RelativeLayout'要求其子女具有在他們指定的放置規則,否則他們將在左上角一個在另一個頂部。你可以使用id或標籤,我會使用id,因爲我認爲它更乾淨。 – Luksprog 2013-04-30 19:35:50