24

我知道以下帖子: 在单个活动中使用多个片段

我正在寻找的是针对特定问题的特定答案。以下代码的结果是一个空白的 FragmentActivity。我在以下代码中缺少什么以使其呈现具有两个片段的活动。一个是空列表片段,另一个是水平布局中包含输入框和按钮的片段,(此布局可在http://developer.android.com/training/basics/firstapp/starting-找到activity.html),我想绝对放置在屏幕底部,固定高度约为 25 dip。

清单.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.my.package"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.my.package.Application"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我的主要活动及其关联的 application.xml 文件。

package com.my.package;

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

public class Application  
extends FragmentActivity 
implements MessageListViewFragment.OnLineSelectedListener, 
SendMessageFragment.OnSendButtonPressed {

    MessageListViewFragment mMessageListFragment;
    SendMessageFragment mSendMessageFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.application);

        mMessageListFragment = new MessageListViewFragment();
        mSendMessageFragment = new SendMessageFragment();

        FragmentTransaction transaction = 
                getSupportFragmentManager().beginTransaction();

        transaction.add(R.id.message_fragment, mMessageListFragment);
        transaction.add(R.id.send_fragment, mSendMessageFragment);

        transaction.commit();
    }

    @Override
    public void onListItemSelected(int position) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSendButtonPressed() {
        // TODO Auto-generated method stub

    }
}

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/message_fragment"
        android:name="com.example.android.fragments.MessageListViewFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="top" />

    <fragment
        android:id="@+id/send_fragment"
        android:name="com.example.android.fragments.SendMessageFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="bottom" />

</LinearLayout>

现在对于两个片段及其关联的 xml 文件:第一个片段(顶部的列表片段)

package com.my.package;

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

public class MessageListViewFragment extends ListFragment {
    OnLineSelectedListener mCallback;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (OnLineSelectedListener)activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnLineSelectedListener");
        }
    }

    // Container Activity must implement this interface
    public interface OnLineSelectedListener {
        public void onListItemSelected(int position);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, 
            ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.list_fragment, null);
    }
}

布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

第二个片段(底部)

package com.my.package;

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

public class SendMessageFragment extends Fragment {
    OnSendButtonPressed mCallback;

    // Container Activity must implement this interface
    public interface OnSendButtonPressed {
        public void onSendButtonPressed();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, 
            ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.input_fragment, null);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (OnSendButtonPressed)activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }
}

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send" />
</LinearLayout>
4

1 回答 1

29

请执行下列操作:

  1. 在主要活动 XML 中更改FragmentFrameLayout两者。
  2. 将主 XML 文件中的两者都layout_width从更改fill_parent为(在步骤 1 中创建的文件)。match_parentFrameLayout
  3. 将主 XML 文件中的两者都layout_height从更改fill_parent为(在步骤 1 中创建的文件)。wrap_contentFrameLayout
  4. 在 List Fragment XML 中更改FrameLayoutListView,因为它是一个 List。
  5. 将 this 的 id 更改LisView@android:id/list,因为ListFragment.

然后让我知道,干杯。

编辑,还有这些:

  1. 更改return inflater.inflate(R.layout.list_fragment, null);return inflater.inflate(R.layout.list_fragment, container, false);
  2. 更改return inflater.inflate(R.layout.input_fragment, null);return inflater.inflate(R.layout.input_fragment, container, false);

编辑:

使您的主要活动 XML 文件如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/message_fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@+id/send_fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

我取出了,android:name"..."因为我不知道那是什么,也无法找出它是什么,如果您确定它的作用,只需将其添加回来,应该没问题。

于 2013-07-10T21:37:07.163 回答