较低 API 上的 Android 片段 [英] Android Fragments on lower API

查看:22
本文介绍了较低 API 上的 Android 片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我开始了解 Android Fragments 但这对我来说仍然令人困惑.我需要一点帮助.正如它所说,自 API 级别 11 起支持 android 片段,但您可以下载support.v4"库包用于较低级别的 API.好吧,首先我创建了一个新项目来尝试 API 级别 15 上的片段.然后我对 API 级别 8 做了同样的事情,但它不起作用......我导入了外部 jar,它看到了所有需要的导入......似乎是什么是这里的问题吗?

well I'm starting to understand Android Fragments but this is still confusing for me. I need a little help. As it says android fragments is supported since API level 11 but you can download "support.v4" libraries pack for lower level APIs. Well first I create new project to try fragments on API level 15. Then I did the same with API level 8 and it does not work... I imported external jar and it sees all needed import as it should... What seems to be the problem here?

这是我的主要课程:

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

public class Fragment2Activity extends FragmentActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

我的主要布局 XML:

My main layout XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

        <fragment 
   android:name="com.frag.Fragment2.frag"
   android:id="@+id/frag"
   android:layout_width="match_parent"
   android:layout_height="match_parent" />

</LinearLayout>

我的片段类:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class frag extends Fragment {
       @Override
       public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          View v = inflater.inflate(R.layout.hello_fragment, container, false);
          return v;
       }
    }

还有我的片段布局 XML:

And my fragment layout 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" >
    <TextView
   android:layout_width="fill_parent"
   android:layout_height="match_parent"
   android:text="THIS IS FRAGMENT"
   android:background="@drawable/ic_launcher"
/>

</LinearLayout>

已修复

推荐答案

您仍在使用 android.view.fragment 类,该类不存在.您必须在旧设备上切换到 android.support.v4.app.fragment.

You are still using the android.view.fragment class, which does not exist. You have to switch to android.support.v4.app.fragment on older devices.

这篇关于较低 API 上的 Android 片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆