Android的两个片段在同一个活动 [英] Android Two Fragments in Same Activity
问题描述
我知道以下职位: <一href="http://stackoverflow.com/questions/4949216/using-multiple-fragments-in-an-single-activity">Using在一个活动的多个片段
我在找的是一个明确的答案,以一个具体的问题。以下code的结果是一个空白FragmentActivity。我是什么在我的下面code缺失得到它呈现的活动有两个片段。一个是空列表片段,另一种是它包含一个输入框,并在一个水平布局的一个按钮,一个片段(这种布局可以在<一个中找到href="http://developer.android.com/training/basics/firstapp/starting-activity.html">http://developer.android.com/training/basics/firstapp/starting-activity.html)我想绝对放置在屏幕的底部为约25浸一个固定的高度。
的Manifest.xml
&LT; XML版本=1.0编码=UTF-8&GT?;
&LT;舱单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.my.package
安卓版code =1
机器人:VERSIONNAME =1.0&GT;
&LT;用途-SDK
安卓的minSdkVersion =8
机器人:targetSdkVersion =17/&GT;
&lt;应用
机器人:allowBackup =真
机器人:图标=@可绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme&GT;
&LT;活动
机器人:名称=com.my.package.Application
机器人:标签=@字符串/ APP_NAME&GT;
&LT;意向滤光器&gt;
&lt;作用机器人:名称=android.intent.action.MAIN/&GT;
&LT;类机器人:名称=android.intent.category.LAUNCHER/&GT;
&所述; /意图滤光器&gt;
&LT; /活性GT;
&LT; /用途&gt;
&LT; /舱单&GT;
我的主要活动及其关联的application.xml文件。
包com.my.package;
进口android.os.Bundle;
进口android.support.v4.app.FragmentActivity;
进口android.support.v4.app.FragmentTransaction;
公共类应用
扩展FragmentActivity
实现MessageListViewFragment.OnLineSelectedListener,
SendMessageFragment.OnSendButton pressed {
MessageListViewFragment mMessageListFragment;
SendMessageFragment mSendMessageFragment;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.application);
mMessageListFragment =新MessageListViewFragment();
mSendMessageFragment =新SendMessageFragment();
FragmentTransaction交易=
。getSupportFragmentManager()的BeginTransaction();
transaction.add(R.id.message_fragment,mMessageListFragment);
transaction.add(R.id.send_fragment,mSendMessageFragment);
器transaction.commit();
}
@覆盖
公共无效onListItemSelected(INT位置){
// TODO自动生成方法存根
}
@覆盖
公共无效onSendButton pressed(){
// TODO自动生成方法存根
}
}
布局:
&LT; LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:baselineAligned =假
机器人:方向=垂直&GT;
&LT;片段
机器人:ID =@ + ID / message_fragment
机器人:名称=com.example.android.fragments.MessageListViewFragment
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:重力=顶/&GT;
&LT;片段
机器人:ID =@ + ID / send_fragment
机器人:名称=com.example.android.fragments.SendMessageFragment
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:重力=底部/&GT;
&LT; / LinearLayout中&GT;
和现在的两个片段及其相关的XML文件: 第一块碎片(名单片段顶部)
包com.my.package;
进口android.app.Activity;
进口android.os.Bundle;
进口android.support.v4.app.ListFragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
公共类MessageListViewFragment扩展ListFragment {
OnLineSelectedListener mCallback;
@覆盖
公共无效onAttach(活动活动){
super.onAttach(活动);
//这将确保集装箱活动已实施
//回调接口。如果不是,它抛出一个异常
尝试 {
mCallback =(OnLineSelectedListener)的活动;
}赶上(ClassCastException异常E){
抛出新ClassCastException异常(activity.toString()
+必须实现OnLineSelectedListener);
}
}
//集装箱活动必须实现此接口
公共接口OnLineSelectedListener {
公共无效onListItemSelected(INT位);
}
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
}
@覆盖
公共查看onCreateView(LayoutInflater充气,
ViewGroup中的容器,包savedInstanceState){
返回inflater.inflate(R.layout.list_fragment,NULL);
}
}
布局:
&LT;的FrameLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / fragment_container
机器人:layout_width =match_parent
机器人:layout_height =match_parent/&GT;
第二个片段(底部)
包com.my.package;
进口android.app.Activity;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
公共类SendMessageFragment扩展片段{
OnSendButton pressed mCallback;
//集装箱活动必须实现此接口
公共接口OnSendButton pressed {
公共无效onSendButton pressed();
}
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
}
@覆盖
公共查看onCreateView(LayoutInflater充气,
ViewGroup中的容器,包savedInstanceState){
返回inflater.inflate(R.layout.input_fragment,NULL);
}
@覆盖
公共无效onAttach(活动活动){
super.onAttach(活动);
//这将确保集装箱活动已实施
//回调接口。如果不是,它抛出一个异常
尝试 {
mCallback =(OnSendButton pressed)的活动;
}赶上(ClassCastException异常E){
抛出新ClassCastException异常(activity.toString()
+必须实现OnHeadlineSelectedListener);
}
}
}
布局:
&LT; LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=横向&GT;
&LT; EditText上机器人:ID =@ + ID / edit_message
机器人:layout_weight =1
机器人:layout_width =0dp
机器人:layout_height =WRAP_CONTENT
机器人:提示=@字符串/ edit_message/&GT;
&LT;按钮
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=@字符串/ button_send/&GT;
&LT; / LinearLayout中&GT;
执行以下操作:
- 修改
片段
到的FrameLayout
中的主要活动XML,这两个。 - 修改
layout_width
从FILL_PARENT
到match_parent
,为无论的FrameLayout
主要XML文件中,(在步骤1中创建的)。 - 修改
layout_height
从FILL_PARENT
到WRAP_CONTENT
,为无论的FrameLayout
主要XML文件中,(在步骤1中创建的)。 - 修改
的FrameLayout
到的ListView
在列表片段XML,因为它是一个列表。 - 在这个变革的ID
LisView
到@android:ID /列表
,因为它是需要的ListFragment
。
然后让我知道,干杯。
编辑,也是这些:
- 修改
返回inflater.inflate(R.layout.list_fragment,NULL);
到返回inflater.inflate(R.layout.list_fragment,集装箱,假);
- 修改
返回inflater.inflate(R.layout.input_fragment,NULL);
到返回inflater.inflate(R.layout.input_fragment,集装箱,假);
编辑:
请您的主要活动的XML文件是这样的:
&LT; RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent&GT;
&LT;的FrameLayout
机器人:ID =@ + ID / message_fragment
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT/&GT;
&LT;的FrameLayout
机器人:ID =@ + ID / send_fragment
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真/&GT;
&LT; / RelativeLayout的&GT;
我拿出安卓名...
,因为我不知道那是什么,也没有能够找出它是什么,如果你知道什么它是肯定的,只是添加回去,应该没问题。
I am aware of the following post: Using Multiple Fragments in an single activity
What I am looking for is a specific answer to a specific problem. The result of the following code is a blank FragmentActivity. What am I missing in my following code to get it to render an activity with two fragments. One is a empty list fragment, the other is a fragment which contains a input box and a button in a horizontal layout, (this layout can be found at http://developer.android.com/training/basics/firstapp/starting-activity.html) that I want to be placed absolutely at the bottom of the screen with a fixed height of about 25 dip.
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>
My main activity and its associated application.xml file.
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
}
}
Layout:
<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>
And now for the two fragments and their associated xml files: First Fragment (list fragment at top)
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);
}
}
Layout:
<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" />
second fragment (at bottom)
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");
}
}
}
Layout:
<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>
Do the following:
- Change
Fragment
toFrameLayout
in the main activity XML, for both. - Change
layout_width
fromfill_parent
tomatch_parent
, for bothFrameLayout
in the main XML file, (ones created in step 1). - Change
layout_height
fromfill_parent
towrap_content
, for bothFrameLayout
in the main XML file, (ones created in step 1). - Change
FrameLayout
toListView
in the List Fragment XML because it is a List. - Change the id of this
LisView
to@android:id/list
, because it is needed for theListFragment
.
Then let me know, Cheers.
Edit, also these:
- Change
return inflater.inflate(R.layout.list_fragment, null);
toreturn inflater.inflate(R.layout.list_fragment, container, false);
. - Change
return inflater.inflate(R.layout.input_fragment, null);
toreturn inflater.inflate(R.layout.input_fragment, container, false);
Edit:
Make your main activity XML file like this:
<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>
I took out the android:name"..."
because I don't know what that is nor able to find out what it is, if you know what it does for sure, just add it back, should be okay.
这篇关于Android的两个片段在同一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!