我想一個片段添加到我的主要活動,所以我有這樣的片段類(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