我想一个片段添加到我的主要活动,所以我有这样的片段类(TitleFragment.java):如何将Android片段添加到活动?
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似乎表明它是为了扩大片段视图而做的,但我不确定。
顺便说一句,这个片段的原因是有每个页面上存在的应用程序徽标(图像和一些可更新的文本),这是做这种事情的好方法吗?对不起,我的新手问题。
安置自己的logcat的堆栈跟踪。而Action Bar已经有了可以更改的应用程序徽标和文本。 – 2013-02-19 04:10:58
android.developers网站有很好的指导。 :::没有这样的事情,只是添加一个片段,通常片段是根据屏幕大小和方向创建的,否则必须创建一个等效的活动。在指南中您将找到代码和解释。 – ilomambo 2013-02-19 04:41:16