2017-08-16 87 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);