片段在运行时显示为保留

时间:2016-12-06 07:48:52

标签: android

我希望我的Fragment看起来像 this。但在我的手机上,它看起来像 this

在代码中:

fragment_q.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">
        <ImageView
            android:paddingTop="100dp"
            android:id="@+id/imageView"
            android:scaleType="fitStart"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </ScrollView>

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_weight="0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="6dp">
        <RadioButton
            android:id="@+id/radio0"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Answer_1" />
        <RadioButton
            android:id="@+id/radio1"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Answer_2" />
        <RadioButton
            android:id="@+id/radio2"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Answer_3" />
        <RadioButton
            android:id="@+id/radio3"
            android:layout_weight="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Answer_4" />
    </RadioGroup>
</LinearLayout>

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.hyeonseok.gumpulza_v005.MainActivity">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/fragment_Question"
        android:name="com.example.hyeonseok.gumpulza_v005.FragmentQuestion"/>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="14dp"
        android:layout_weight="0">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btnPrevious"
                android:text="@string/ButtonPrevious"
                android:layout_gravity="start"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btnNext"
                android:text="@string/ButtonNext"
                android:layout_gravity="end"/>

    </FrameLayout>
</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

        FragmentManager fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.add(R.id.fragment_Question, new FragmentQuestion());
        fragmentTransaction.commit();

    }
}

FragmentQuestion.java

public class FragmentQuestion extends Fragment {

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

        return inflater.inflate(R.layout.fragment_q, container, false);
    } 
}

可以采取哪些措施来解决这个问题?

1 个答案:

答案 0 :(得分:0)

布局:

 <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.hyeonseok.gumpulza_v005.MainActivity">

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment_Question"
    android:alignParentBottom="true"

    />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="14dp"
        android:alignParentTop="true"
        android:id="@+id/flayout"
        >
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btnPrevious"
                android:text="@string/ButtonPrevious"
                android:layout_gravity="start"/>
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/btnNext"
                android:text="@string/ButtonNext"
                android:layout_gravity="end"/>

    </FrameLayout>
</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

    FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    fragmentTransaction.add(R.id.fragment_Question, new FragmentQuestion());
    fragmentTransaction.commit();

}
 }