我使用'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世界文本。
答案 0 :(得分:2)
这很简单。插入Fragment的布局不是您用作Container的视图。事实上,它本身就是碎片。
因此要么使用FrameLayout
作为片段容器,要么将片段保留在XML中,不要调用片段管理器插入此片段。我会建议第一个。
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>