2017-02-19 83 views
-1

即时尝试使用setContentView切换到其中包含片段的布局 但每次都崩溃 我应该怎么做? 这是我的整个代码: (当我点击activity_main中的按钮时,它必须切换到fragment_layout) (fragmant_layout有一个片段,分段的布局是layout1) (当我点击按钮切换时崩溃)将视图更改为android中的片段视图

主要活动:

public class MainActivity extends AppCompatActivity { 

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

public void switchlayout(View view) {//when the button in clicked 
    setContentView(R.layout.fragment_layout); 
} 

} 

片段类:

public class frgment extends Fragment { 
@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.layout1,container,false); 
} 
} 

activity_main.xml中:

<?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" 
tools:context=".fragmenttest.MainActivity"> 




<Button 
    android:text="Button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:onClick="switchlayout" 
    android:layout_marginTop="74dp" 
    android:id="@+id/button" /> 
</RelativeLayout> 

fragment_layout.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:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<fragment 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:name="alirezamellat.fragmenttest.frgment" 
    ></fragment> 

</LinearLayout> 

layout1.xml:

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

</LinearLayout> 
+1

使用片段经理和膨胀您的片段父视图。请看看android :: 提供的资源https://developer.android.com/guide/components/fragments.html – Bayou

回答

0

一旦活动的布局已经被设置它可以在稍后的时间被改变。
有很多方法可以实现这一目标,最简单的方法是使用FragmentManager类来创建包装布局,然后在其中注入片段。

<?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" 
tools:context=".fragmenttest.MainActivity"> 

<FrameLayout 
    android:id="@+id/wrapper" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</FrameLayout> 
<Button 
    android:text="Button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:onClick="switchlayout" 
    android:layout_marginTop="74dp" 
    android:id="@+id/button" /> 
</RelativeLayout> 

然后在您的按钮上单击添加您的片段。

getSupportFragmentManager().beginTransaction().add(R.id.wrapper, MyFragment.newInstance(), "FragTag").commit(); 

请记住,您必须照顾好您的按钮,因为它仍然可以在您的顶部看到片段。

2

请考虑以下几点::

  1. 在你的父母布局(activity_main.xml中),相对布局内,加叫的FrameLayout另一个布局。
  2. 在你的片段布局(fragment_layout.xml),删除片段,并添加layout1.xml的布局,以fragment_layout.xml
  3. 在您的片段类(MyFragment),膨胀fragment_layout.xml到容器并返回视图(类名应以大写字母开头,因此将其命名为MyFragment)
  4. 在Activity类上,将以下代码放置在onclick监听器上。

    //onClick on OnClickListener 
    
    MyFragment fragment = new MyFragment(); 
    getSupportFragmentManager().beginTransaction() 
        .add(R.id.frame_layout, fragment) 
        .commit();