我想将一个片段添加到我的主活动中,因此我有这个片段类(TitleFragment.java):
package com.myapp.app1;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TitleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_title, container, false);
}
}
接下来是我的碎片的实际内容,包含在
fragment_title.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" >
<ImageView
android:id="@+id/logo_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo_main"
android:contentDescription="@string/app_name"
android:scaleType="fitStart"
android:adjustViewBounds="true" />
</LinearLayout>
在我的 activity_main.xml
文件中,我有以下代码段(除了常规内容):
<fragment
android:name="com.myapp.app1.TitleFragment"
android:id="@+id/title_fragment"
android:layout_width="0dp"
android:layout_height="match_parent" />
这种方法创建片段是否正确?我的应用程序崩溃了,LogCat似乎表明与充气片段视图有关,但我不确定。
顺便说一下,这个片段的原因是拥有应用程序徽标(图像和一些可更新文本),存在于每个页面上,这是做这样的事情的好方法吗?抱歉我的新手问题。