当我尝试在Eclipse的图形布局中显示一个android xml文件时,我得到以下消息:从Fragment Layout上下文菜单中选择预览布局。我通过右键单击布局来遵循说明 - >片段布局 - >选择布局。但是,当我尝试选择我的布局(由于某种原因无法显示的文件)时,我收到此错误:“循环包含,无效”。我可以选择所有其他布局,例如androids预定义的list_content。
我做错了什么?
这是我的xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/example_id"
android:name="com.example.test.MainFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
这是我的java:
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
这是一个打印屏幕:
答案 0 :(得分:1)
您应该在片段XML项中包含name属性,例如:
android:name="com.example.news.ArticleListFragment"
...其中“ArticleListFragment”是您要使用的Fragment类。完整片段是:
<fragment android:name="com.example.news.ArticleListFragment"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
另请参阅:Fragments和Creating a Fragment。
答案 1 :(得分:1)
在@MiStr指出正确的方向后,在他的回答的评论中,我明白我得到循环包含错误的原因是非常明显的。我试图将片段的预览布局设置为包含片段的视图。预览布局应该是片段的根布局,而不是片段本身......根布局是片段onCreateView()
方法返回的布局。更多信息http://developer.android.com/guide/components/fragments.html#Creating
ListFragment会自动创建根布局,这样您就不必重写onCreateView()方法。 Eclipse(我不知道android studio)应该自动预览ListFragment的样本列表。
答案 2 :(得分:0)
只导入android.app.Fragment;可以预览! import android.support.v4.app.Fragment;无法预览!