我已经使用'developer.android.com'教程学习了android应用开发两天了。目前,我正在学习片段和框架集的概念。我已经使用片段编写了一个hello world程序。但是问题是我的片段布局两次插入到片段容器中,结果是框架集中有2个重叠的片段。你能告诉我为什么会这样吗,我的代码有什么问题吗?
以下是我的Home_page.java(包含fragment_container的活动)
package com.technology.computer.mit.ctechmit;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class Home_page extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
// Create a new Fragment to be placed in the activity layout
Home_pageFragment firstFragment = new Home_pageFragment();
// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home_page, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
下面是我的Home_pageFragment.java(应该插入容器的片段)
package com.technology.computer.mit.ctechmit;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A placeholder fragment containing a simple view.
*/
public class Home_pageFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home_page, container, false);
}
}
以下是我的activity_home_page.xml(fragment_container布局)
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_container"
android:name="com.technology.computer.mit.ctechmit.Home_pageFragment"
tools:layout="@layout/fragment_home_page"
android:layout_width="match_parent"
android:layout_height="match_parent" />
以下是我的fragment_home_page.xml(片段布局)
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context=".Home_pageFragment">
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send" />
</LinearLayout>
您能帮我解决这个错误吗?
为什么片段在家庭活动中被加载两次?
我收到了重叠的Hello World文字。
最佳答案
这很简单。您要插入片段的布局不是用作容器的视图。实际上,它本身就是碎片。
因此,要么将FrameLayout
用作片段容器,要么将片段保留在XML中,并且不要调用片段管理器来插入该片段。我建议第一个。
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>