2016-01-23 116 views
1

我創建了一個帶有可滾動選項卡的項目。每個選項卡都由一個片段聲明。現在我想要f.e.片段1有8個按鈕,Fragment2 4個按鈕等等。我想要有行,每個包含最多三個按鈕,所以如果我產生4個按鈕,應該有一行3個按鈕和一行下面有1個按鈕。因此,這是如何使滾動標籤:以編程方式將按鈕添加到片段

我Main_Activity.java

public class MainActivity extends FragmentActivity { 
    ViewPager viewPager = null; 
    SoundPlayer sp = new SoundPlayer(); 
    protected void onCreate(Bundle savedInstanceStats) 
    { 
     super.onCreate(savedInstanceStats); 
     setContentView(R.layout.homescreen); 
     viewPager = (ViewPager) findViewById(R.id.pager); 
     FragmentManager fragmentManager = getSupportFragmentManager(); 
     viewPager.setAdapter(new MyAdapter(fragmentManager)); 
    } 
} 

class MyAdapter extends FragmentPagerAdapter 
{ 

    public MyAdapter(FragmentManager fm) { 
     super(fm); 

    } 

    @Override 
    public Fragment getItem(int position) { 
     Fragment fragment = null; 
     if(position == 0) 
      fragment = new FragmentDominik(); 
     else if(position == 1) 
      fragment = new FragmentTobias(); 
     else if(position == 2) 
      fragment = new FragmentTom(); 
     else if(position == 3) 
      fragment = new FragmentNikolas(); 
     else if(position == 4) 
      fragment = new FragmentGroups(); 
     else if(position == 5) 
      fragment = new FragmentOthers(); 
     else if(position == 6) 
      fragment = new FragmentFavorites(); 
     return fragment; 
    } 

    @Override 
    public int getCount() { 
     return 7; 
    } 

    public CharSequence getPageTitle(int position) 
    { 
     String title = new String(); 
     if(position == 0) 
      return "Dominik"; 
     else if(position == 1) 
      return "Tobias"; 
     else if(position == 2) 
      return "Tom"; 
     else if(position == 3) 
      return "Nikolas"; 
     else if(position == 4) 
      return "Groups"; 
     else if(position == 5) 
      return "Others"; 
     else if(position == 6) 
      return "Favorites"; 
     return null; 
    } 
} 

我homescreen.xml

<android.support.v4.view.ViewPager 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:id="@+id/pager" 
    android:layout_height="match_parent"> 

    <android.support.v4.view.PagerTitleStrip 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/title" 
     android:layout_gravity="top" 
     android:paddingTop="4dp" 
     android:paddingBottom="4dp" 
     android:background="#4052b5" 
     android:focusableInTouchMode="true" 
     android:textColor="#ffffff"/> 
</android.support.v4.view.ViewPager> 

一個例子Fragment.java

public class FragmentTobias extends Fragment { 

     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      return inflater.inflate(R.layout.tobias, container, false); 
     } 
    } 

一個例子片段.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#d1d1d1"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:id="@+id/textView3" 
     android:layout_gravity="center_horizontal" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Tobias" 
     android:id="@+id/textView7" 
     android:layout_gravity="center_horizontal" /> 
</LinearLayout> 

所以現在如果我想在tobias.xml中產生8個按鈕的數量,在FragmentTobias.java中大量使用,我該怎麼做?我對創建android應用程序很陌生,所以任何幫助表示讚賞。

EDIT1:

LinearLayout linearLayout = new LinearLayout(getActivity()); 
     LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); 
     linearLayout.setLayoutParams(params1); 
     linearLayout.setOrientation(LinearLayout.HORIZONTAL); //or VERTICAL 

     LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 

     Button button = new Button(getActivity()); 
     button.setLayoutParams(params2); 

     Button button2 = new Button(getActivity()); 
     button2.setLayoutParams(params2); 

     //like this, add all buttons and other views 
     //you can use a loop for adding multiple similar views 

     linearLayout.addView(button); 
     linearLayout.addView(button2); 
     container.addView(linearLayout); 

回答

1

您可以動態地添加按鈕ViewGroup並用它吹你的看法。試試以下代碼,

public class FragmentTobias extends Fragment { 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     LinearLayout linearLayout = new LinearLayout(getActivity()); 
     LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
     linearLayout.setLayoutParams(layoutParams); 
     linearLayout.setOrientation(LinearLayout.HORIZONTAL); //or VERTICAL 

     LayoutParams buttonParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

     Button button = new Button(getActivity()); 
     button.setLayoutParams(buttonParams); 

     Button button2 = new Button(getActivity()); 
     button2.setLayoutParams(buttonParams); 

     linearLayout.addView(button); 
     linearLayout.addView(button2); 

     //like this, add all buttons and other views 
     //you can use a loop for adding multiple similar views 

     container.addView(linearLayout); 
     View view = inflater.inflate(R.layout.tobias, container, false); 

     return view; 
    } 
} 
相關問題