0

So I am very new to the Android DK, and was sailing pretty fine until I started trying to mess with Fragments.

At the core of it, what I'm trying to do is have a series of buttons on my app's screen, and whichever button the user presses will change what text/spinners/buttons display on the screen.

How I decided to implement that was via Fragments. I can't figure out why my Fragment isn't displaying however, this should be a relatively simple example.

I had a activity_main.xml that is relatively simple. I've changed variable names, but see below:

<LinearLayout android:id="@+id/mainActivity" 
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"
android:animateLayoutChanges="true" >

<Button
    android:id="@+id/buttonD"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/d_name"
    android:onClick="dMenuButton" />

<RelativeLayout
    android:id="@+id/fragment_container"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="gone" />

<Button
    android:id="@+id/buttonR"
    android:layout_below="@id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/r_name"
    android:onClick="rButton" />

</LinearLayout>

And the corresponding MainActivity.java

public void dMenuButton(View view){

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.show(dFragment);
    fragmentTransaction.commit();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;

}

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    // remove title
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                         WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_main);

if (savedInstanceState == null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        dFragment = new DFragment();
        fragmentTransaction.add(R.id.fragment_container, dFragment);
        fragmentTransaction.commit();
    }

}

And the fragment_d.xml

<RelativeLayout 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" >

<TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="20dp"
    android:text="@string/text1"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<Spinner
    android:id="@+id/numD"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@id/text1"
    android:layout_toRightOf="@id/text1" />

</RelativeLayout>

With its respective DFragment.java

public class DFragment extends Fragment {

static Spinner dSpinner;
ArrayAdapter<CharSequence> dAdapter;
RelativeLayout view;

public static DFragment newInstance() {
    DFragment dFragment = new DFragment();

    return dFragment ;
}

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

    view = (RelativeLayout) inflater.inflate(R.layout.fragment_d,
                                 container, false);

    createSpinners();

    return view;
}

private void createSpinners() {

    dSpinner = (Spinner) view.findViewById(R.id.numD);
    dAdapter= ArrayAdapter.createFromResource(getActivity().getBaseContext(),
                                                     R.array.numD,
                                                     android.R.layout.simple_spinner_item);

}

}

Currently, the fragment layout is covering up the buttons beneath it. Any help in forcing it to push down the components below would be great.

Thanks in advance.

4

1 回答 1

0

有了fragment_container. 您可以将其保持为持续可见,然后允许片段事务更改该容器中显示的内容。

根据您的要求,您可以简单地执行 a.replace(R.layout.fragment_container, anotherFragment)来更改正在显示的片段(代码只是伪代码)。

至于按钮,您是将它们activity_main.xml添加到片段的布局中还是添加到片段的布局中?只要您fragment_container在主活动布局中的布局之后添加新按钮/文本,就可以了。让我确切地知道您要使用按钮/文本实现的目标!

祝你好运!

于 2013-07-29T23:24:54.983 回答