2017-08-16 85 views
2

我想在選項卡式活動中以編程方式將按鈕添加到我的片段中。不知何故,addView()方法不起作用。沒有錯誤,沒有例外,它只是不顯示按鈕。addView()不顯示片段中的按鈕

這就是我的MainActivity的onCreate()onCreateView()代碼:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Create the adapter that will return a fragment for each of the three 
    // primary sections of the activity. 
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

    // Set up the ViewPager with the sections adapter. 
    mViewPager = (ViewPager) findViewById(R.id.container); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
    tabLayout.setupWithViewPager(mViewPager); 

    //Fragments erstellen 
    MyFragment myfragment = new MyFragment(); 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 

    fragmentTransaction.add(R.id.container, myfragment); 

    fragmentTransaction.commit(); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_my, container, false); 
    return rootView; 
} 

這是我的片段的onCreateView()代碼:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

    // Inflate the layout for this fragment 
    View rootview = inflater.inflate(R.layout.fragment_my, container, 
    false); 
    LinearLayout l_layout = (LinearLayout) 
    rootview.findViewById(R.id.LinLay); 
    Button b = new Button(getActivity()); 
    b.setText("test"); 
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
    b.setLayoutParams(params); 
    l_layout.addView(b); 
    return rootview; 
} 

我想,這只是一個簡單的錯誤,但我可以找不到它...

編輯: 個AndroidManifest.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.user.test2"> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/main_content" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:context="com.example.user.test2.MainActivity"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="@dimen/appbar_padding_top" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:layout_scrollFlags="scroll|enterAlways" 
     app:popupTheme="@style/AppTheme.PopupOverlay"> 

    </android.support.v7.widget.Toolbar> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

</android.support.design.widget.AppBarLayout> 

<android.support.v4.view.ViewPager 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

</android.support.design.widget.CoordinatorLayout> 

fragment_my.xml:

<FrameLayout 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" 
tools:context="com.example.user.test2.layout.MyFragment"> 

<!-- TODO: Update blank fragment layout --> 

<LinearLayout 
    android:id="@+id/LinLay" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"></LinearLayout> 
</FrameLayout> 
+0

發佈您的XML。請 – Leonardo

回答

0

難道不是因爲你設置你的片段到你的Viewpager fragmentTransaction.add(R.id.container, myfragment);

要顯示的片段,你應該:

  • 使用ViewPager adpater
  • 使用另一種容器(如FrameLayout裏)的片段直接添加到添加的片段是
+1

作品,非常感謝! :D - 使用適配器和以下說明添加片段:https://stackoverflow.com/questions/18413309/how-to-implement-a-viewpager-with-different-fragments-layouts – Zimbaa

0

試試這個:

((LinearLayout)l_layout.addView(b);