Android在同一活动中有两个片段

Android在同一活动中有两个片段,android,android-fragments,Android,Android Fragments,我知道以下帖子: 我所寻找的是一个具体问题的具体答案。以下代码的结果是一个空白片段活动。我在下面的代码中遗漏了什么,以便让它呈现一个包含两个片段的活动。一个是空列表片段,另一个是包含一个输入框和一个水平布局中的按钮的片段,(这个布局可以在中找到),我想绝对放置在屏幕底部,固定高度约为25度 Manifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.an

我知道以下帖子:

我所寻找的是一个具体问题的具体答案。以下代码的结果是一个空白片段活动。我在下面的代码中遗漏了什么,以便让它呈现一个包含两个片段的活动。一个是空列表片段,另一个是包含一个输入框和一个水平布局中的按钮的片段,(这个布局可以在中找到),我想绝对放置在屏幕底部,固定高度约为25度

Manifest.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>
布局:

<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>
<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" />
<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>
布局:

<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>
<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" />
<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>
布局:

<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>
<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" />
<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>

执行以下操作:

  • 将主活动XML中的
    Fragment
    更改为
    FrameLayout
  • 将主XML文件(步骤1中创建的文件)中的
    FrameLayout
    layout\u width
    fill\u parent
    更改为
    match\u parent
  • 将主XML文件(步骤1中创建的文件)中的
    FrameLayout
    layout\u height
    fill\u parent
    更改为
    wrap\u content
  • 将列表片段XML中的
    FrameLayout
    更改为
    ListView
    ,因为它是一个列表
  • 将此
    LisView
    的id更改为
    @android:id/list
    ,因为
    ListFragment
    需要它
  • 那就让我知道,干杯

    请编辑以下内容:

  • 更改
    返回充气机。充气(R.layout.list\u片段,空)
    返回充气机。充气(R.layout.list\u碎片,容器,假)
  • 更改
    返回充气机。充气(R.layout.input\u片段,空)
    返回充气机。充气(R.layout.input\u碎片,容器,假)
  • 编辑:

    使您的主要活动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>
    
    
    

    我拿出了安卓:名称“…”
    ,因为我不知道它是什么,也无法找出它是什么,如果你确实知道它的功能,只需把它加回去就可以了。

    好吧,您已经在xml中硬编码了片段,因此您不想在代码中再次添加它们如果我将它们从xml中删除,它会起作用吗?保留xml只需删除代码并查看发生了什么仍然没有运气,我认为这与我对片段布局所做的工作有关。也许是“填充父对象”,我只是不知道如何处理它们。该按钮现在显示在活动的“查看无行编辑”的顶部。将主活动xml文件从片段更改为框架布局如何将其保留为片段。这与FrameLayout上的教程不同,因为它更一般,在任何情况下都不会产生太大影响,google在另一个教程中使用了FrameLayout。我在答案中添加了一个编辑,将第二个布局放在底部。在这里是一个更复杂的布局,他们使用FrameLayout作为一个片段的窗口。您的解决方案似乎适用于Nexus平板电脑,但似乎不适用于我的三星S3。这有什么原因吗?据我所知,你在S3上得到了什么?另外,尝试重新构建应用程序,并可能卸载它并在S3上重新安装,这可能会有所不同。