我正在尝试做动态片段布局。我正在使用 2 个 xml 文件:一个是 ,main_layout
另一个是fragment1
.
我使用了,Log.i
所以我可以知道我的程序在哪里崩溃,但它引用了运行代码正常但它没有出现在 main_layout 上的 fragment1。
这是我的代码:
main_layout.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/testFragment"
android:visibility="gone" >
</FrameLayout>
片段.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"
android:id="@+id/fragmenta"
android:background="#F000F0">
<TextView
android:id="@+id/fragmentatext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragmenta 1" />
</LinearLayout>
这是我Side_Fragment
的扩展SherlockFragment
:
View view;
private final String TAG = "Side_Fragment";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragmenta, container, false);
Log.i(TAG,"fragment installed");
return view;
}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
}
}
这是主要活动:
private final String TAG = "Main Activity";
FragmentManager fm = getSupportFragmentManager();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
Log.i(TAG,"OnCreate");
Button add = (Button) findViewById(R.id.button1);
Log.i(TAG,"called FragmentManager");
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.twitter:
Log.i(TAG,"Twitter is clicked");
// Check to see if the Fragment back stack has been populated // If not, create and populate the layout.
Fragment detailsFragment = (Side_Fragment)fm.findFragmentById(R.id.fragmenta);
Log.i(TAG,"called FragmentManager");
if (detailsFragment == null) {
Log.i(TAG,"pass ifstatment");
FragmentTransaction ft = fm.beginTransaction();
Log.i(TAG,"called FragmentTransaction");
Side_Fragment test = new Side_Fragment();
ft.add(R.id.testFragment,test);
Log.i(TAG,"called add");
ft.addToBackStack(null);
ft.commit();
Log.i(TAG,"called commit");
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
}
return true;
case R.id.facebook:
Log.i(TAG,"Facebook is clicked");
return true;
case R.id.refresh:
Log.i(TAG,"Refresh is clicked");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
final MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.i(TAG,"onPause");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.i(TAG,"onResume");
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.i(TAG,"onStart");
}
}