嗨我这个类型的编程有一个大问题我无法打开布局成片段,这是我的代码。 我需要知道我必须插入main.java以打开tooldadi.java和toolnomi.java以获得正确的布局。非常感谢这个网站是最好的。
tooldadi.java
Please check the result in console.
toolnomi.java
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by ferdinandoargenzio on 23/07/15.
*/
public class tooldadi extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(
R.layout.tooldadi_layout, container, false);
}
}
tooldadi_layout.xml
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by ferdinandoargenzio on 23/07/15.
*/
public class toolnomi extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(
R.layout.toolnomi_layout, container, false);
}
}
tooldadi_layout.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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tool dadi"
android:id="@+id/textView"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Main.java
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tool nomi"
android:id="@+id/textView2"
android:layout_gravity="center_horizontal" />
</LinearLayout>
答案 0 :(得分:0)
我认为你让事情变得复杂。而且我没有看到片段的绑定实例到Main。进行以下更改:
@Override
public void onNavigationDrawerItemSelected(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new tooldadi();
mTitle = getString(R.string.title_section1);
break;
case 1:
fragment = new toolnomi();
mTitle = getString(R.string.title_section2);
break;
case 2:
mTitle = getString(R.string.title_section3);
break;
case 3:
mTitle = getString(R.string.title_section4);
break;
case 4:
mTitle = getString(R.string.title_section5);
break;
case 5:
mTitle = getString(R.string.title_section6);
break;
}
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
但是,我建议对单个片段使用NewInstance
方法,而不是直接在其上调用new
以避免间歇性崩溃。