【发布时间】:2013-02-03 16:11:08
【问题描述】:
我想在我的主要活动中添加一个片段,所以我有这个片段类(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 中,我在活动的常规内容中有这个 sn-p:
<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 已经有您可以更改的应用徽标和文本。
-
android.developers 网站有一个很好的指南。 ::: 没有“只是”添加片段这样的事情,通常片段是根据屏幕大小和方向创建的,否则必须创建等效的活动。在指南中,您将找到代码和解释。
标签: android xml layout android-activity fragment