片段不会显示在Activity中

时间:2015-03-19 12:48:57

标签: android android-layout android-fragments

我正在尝试创建一个屏幕,其中包含一个带有3个按钮的片段,下面是一个文本框架,但在运行应用程序时看不到任何内容。 我是Android的新手,所以我想我错过了一些基本的东西。 我错过了什么?

编辑:谢谢你的所有答案。问题最终是按钮的大小不可见。 该活动确实加载了片段,而无需调用片段管理器。

哦,学习新框架的乐趣:)

谢谢!

activity_main.xml中

<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:orientation="vertical">

<fragment
    android:id="@+id/buttons"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    class="com.thenoisemargins.eyaldagiavdor.thisdaybefore.ButtonsFragment" />

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp">

    <TextView
        android:layout_width="48dp"
        android:layout_height="45dp"
        android:id="@+id/historicalQuoteTextView"
        android:layout_gravity="center_vertical"
        android:textAlignment="center"
        android:textColor="#FFFFFFFF"
        android:text="Quote"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</FrameLayout>

buttons_fragment.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<at.markushi.ui.CircleButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:text="@string/this_day_before"
    android:id="@+id/thisDayBeforeButton"
    app:cb_color="#99CC00"
    app:cb_pressedRingWidth="8dip" />

<at.markushi.ui.CircleButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:text="@string/choose_date"
    android:id="@+id/ChooseDateButton"
    app:cb_color="#99CC00"
    app:cb_pressedRingWidth="8dip" />

<at.markushi.ui.CircleButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:text="@string/random_date"
    android:id="@+id/RandomDateButton"
    app:cb_color="#99CC00"
    app:cb_pressedRingWidth="8dip" />
</LinearLayout>

MainActivity.java

public class MainActivity extends ActionBarActivity {

    private ButtonsFragment mFragment;

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

        /*
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        mFragment =  new ButtonsFragment();
        fragmentTransaction.add(R.id.buttons, mFragment);
        fragmentTransaction.commit();
               */

        mFragment = (ButtonsFragment) getFragmentManager()
                .findFragmentById(R.id.buttons);


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

ButtonsFragment.java

public class ButtonsFragment extends Fragment {

private at.markushi.ui.CircleButton mThisDayBeforeButton;
private at.markushi.ui.CircleButton mChooseDateButton;
private at.markushi.ui.CircleButton mRandomDateButton;


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

    View rootView = inflater.inflate(R.layout.buttons_fragment, container, false);



    return rootView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    mThisDayBeforeButton = (at.markushi.ui.CircleButton) getActivity().findViewById(R.id.thisDayBeforeButton);
    mChooseDateButton = (at.markushi.ui.CircleButton) getActivity().findViewById(R.id.ChooseDateButton);
    mRandomDateButton = (at.markushi.ui.CircleButton) getActivity().findViewById(R.id.RandomDateButton);

}

}

3 个答案:

答案 0 :(得分:2)

删除给定部分的评论。您必须使用fragmentTransaction在fragmentManager中添加片段才能查看片段。

  mFragment = (ButtonsFragment) getFragmentManager()
                .findFragmentById(R.id.buttons);

            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            mFragment =  new ButtonsFragment();
            fragmentTransaction.add(R.id.buttons, mFragment);
            fragmentTransaction.commit();

答案 1 :(得分:0)

如果工作,请尝试此操作

FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            mFragment =  new ButtonsFragment();
            fragmentTransaction.add(R.id.buttons, mFragment);
            fragmentTransaction.commit();

以上代码替换为此,尝试可能有效。

FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.buttons, fragment).commit();

答案 2 :(得分:0)

如果你想直接在xml中显示片段,那么

<fragment
android:id="@+id/buttons"
android:layout_width="match_parent"
android:layout_height="400dp"
class="com.thenoisemargins.eyaldagiavdor.thisdaybefore.ButtonsFragment" />

或者如果要以编程方式显示,则需要使用

  

getSupportFragmentManager()

因为您正在使用ActionbarActivity,例如: 在布局中添加带有id'frame'(示例)的framelayou,然后将此代码添加到您的活动

FrameLayout f = (FrameLayout) findViewById(R.id.fram);
    getSupportFragmentManager().beginTransaction().replace(R.id.fram, new ButtonsFragment()).commit();

希望这有帮助!