【问题标题】:Fragment is not shown on Activity片段未显示在活动中
【发布时间】:2015-06-26 10:20:55
【问题描述】:

在我的应用程序中,一个 Activity 必须包含三个片段,其中一个始终保留在 Activity 上,而另外两个必须更改位置。但是片段不会显示在活动上。

我在这里的某个地方读到我应该将 LinearLayout 更改为 FragmentLayout 但它不起作用。

public class MainActivity extends ActionBarActivity {

private final Fragment_1to6 f1to6 = new Fragment_1to6();
private final Fragment_7to12 f7to12 = new Fragment_7to12();

private FragmentManager mFragmentManager;
private FrameLayout mButtonsLayout, mLayout1to6;
private FrameLayout mLayout7to12;

private static final int MATCH_PARENT = LinearLayout.LayoutParams.MATCH_PARENT;
private static final int WRAP_CONTENT = LinearLayout.LayoutParams.WRAP_CONTENT;

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


    mButtonsLayout = (FrameLayout) findViewById(R.id.fragment_buttons);
    mLayout1to6 = (FrameLayout) findViewById(R.id.fragment_1to6);
    mLayout7to12 = (FrameLayout) findViewById(R.id.fragment_7to12);

    mFragmentManager = getFragmentManager();
    final FragmentTransaction ft = mFragmentManager.beginTransaction();

    ft.add(R.id.fragment_buttons, new buttonsFragment());
    ft.add(R.id.fragment_1to6, f1to6);

    ft.addToBackStack("fragment_buttons");
    ft.addToBackStack("fragment_1to6");

    ft.commit();

    mFragmentManager
            .addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
                public void onBackStackChanged() {
                    setLayout();
                }
            });


    mFragmentManager.executePendingTransactions();


}

private void setLayout() {

    // Determine whether the Fragment has been added
    if (!f7to12.isAdded()) {

        mButtonsLayout.setLayoutParams(new LinearLayout.LayoutParams(WRAP_CONTENT,
                MATCH_PARENT, 1f));
        mLayout1to6.setLayoutParams(new LinearLayout.LayoutParams(WRAP_CONTENT,
                MATCH_PARENT, 6f));
    } else {

        mButtonsLayout.setLayoutParams(new LinearLayout.LayoutParams(WRAP_CONTENT,
                MATCH_PARENT, 1f));
        // Make the TitleLayout take 6/13 of the layout's width
        mLayout1to6.setLayoutParams(new LinearLayout.LayoutParams(WRAP_CONTENT,
                MATCH_PARENT, 6f));

        // Make the QuoteLayout take 6/13's of the layout's width
        mLayout7to12.setLayoutParams(new LinearLayout.LayoutParams(WRAP_CONTENT,
                MATCH_PARENT, 6f));
    }


}

我该如何解决?

【问题讨论】:

  • 请附上您的布局代码。

标签: android android-fragments android-activity fragment android-linearlayout


【解决方案1】:

我有 TableRows,这就是原因,现在我删除了它们并制作了 LinearLayout,它显示在片段的预览中,但没有显示在 activity_main。这是主要活动的 xml 代码。以下是片段活动的xlm代码。我通过调用向活动添加片段:mButtonsFragment = (buttonsFragment) mFragmentManager.findFragmentById(R.id.fragment_buttons);

    <LinearLayout
    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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="horizontal">


    <include
        android:id="@+id/app_bar"
        layout="@layout/app_bar" />


    <fragment
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/fragment_buttons"
        class="com.example.user.timeline.buttonsFragment"
        tools:layout="@layout/fragment_buttons"
        android:layout_weight="1"
        android:layout_gravity="left">

    </fragment>



    <FrameLayout

    android:layout_width="match_parent"
    android:layout_height="match_parent"
        class="com.example.user.timeline.Fragment_1to6"
    android:id="@+id/fragment_1to6"
        tools:layout="@layout/fragment_buttons"
        android:layout_weight="3">
</FrameLayout>


    <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment_7to12"
        android:layout_weight="3">
</FrameLayout>


</LinearLayout>





<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    tools:context="com.example.user.timeline.buttonsFragment"
    android:orientation="vertical"
   >


    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button"
            android:text="ADD ACTIVITY"
            android:width="40dp"
            android:height="30dp"
            android:background="#B6B6B6"
            android:textColor="#000000"
            android:onClick="onClick"

             />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ADD ACTIVITY"
            android:id="@+id/button2"
            android:width="40dp"
            android:height="30dp"
            android:background="#B6B6B6"
            android:textColor="#000000"
            android:onClick="onClick" />



        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ADD ACTIVITY"
            android:id="@+id/button3"
            android:layout_column="0"
            android:width="40dp"
            android:height="30dp"
            android:background="#B6B6B6"
            android:textColor="#000000"
            android:onClick="onClick" />

</LinearLayout>

【讨论】:

  • 哦,现在我将 main xml 更改为 FrameLayout,它可以工作了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多