2016-12-06 58 views
0

我想我的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); 
    } 
} 

可以做些什麼來解決這個問題呢?

回答

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(); 

} 
} 
+0

它的工作原理!謝謝Rushi。你是我的救星。 –

+0

很高興幫助你。接受我的接受 –

+0

我接受了。感謝agian :) –