Android 如何在单个活动中获得2个片段
我试图在android中使用片段,并希望同时显示Android 如何在单个活动中获得2个片段,android,android-studio,android-fragments,android-activity,Android,Android Studio,Android Fragments,Android Activity,我试图在android中使用片段,并希望同时显示fragment1和fragment2,但通过这段代码,我只得到了第二个片段。如何获取这两个片段 import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; imp
fragment1
和fragment2
,但通过这段代码,我只得到了第二个片段。如何获取这两个片段
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.WindowManager;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm=getFragmentManager();
FragmentTransaction ft= fm.beginTransaction();
Fragment1 f1 = new Fragment1();
ft.add(android.R.id.content, f1);
Fragment2 f2 = new Fragment2();
ft.add(android.R.id.content, f2);
ft.commit();
}
}
试试这个逻辑:
在主活动布局中添加两个framelayout
:一个接一个
<?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"
android:weightSum="2">
<FrameLayout
android:id="@+id/fragment1"
android:name="com.example.FragmentOne"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/fragment2"
android:name="com.example.FragmentTwo"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
试试这个逻辑:
在主活动布局中添加两个framelayout
:一个接一个
<?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"
android:weightSum="2">
<FrameLayout
android:id="@+id/fragment1"
android:name="com.example.FragmentOne"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/fragment2"
android:name="com.example.FragmentTwo"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
如何显示2个片段?一个比另一个高?请详细解释。您希望如何显示2个片段?一个比另一个高?请详细解释。当我使用fm.beginTransaction().replace(R.id.fragment1,one,“fragmentone”).commit()时;fm.beginTransaction().replace(R.id.fragment2,two,“fragmenttwo”).commit();然后应用程序正在运行,但当我使用FragmentManager fm=getFragmentManager()时;FragmentTransaction ft=fm.beginTransaction();ft.replace(R.id.fragment1,一,“fragmentone”).commit();ft.replace(R.id.fragment2,2,“fragment2”).commit();然后应用程序崩溃了。为什么?我不明白你的意思…………如果你使用的是v4版本,什么是片段事务版本检查?还要发布崩溃日志..没有日志就不容易知道崩溃原因..当我使用fm.beginTransaction()时.替换(R.id.fragment1,one,“fragmentone”).commit();fm.beginTransaction().replace(R.id.fragment2,two,“fragmenttwo”).commit();然后应用程序正在运行,但当我使用FragmentManager fm=getFragmentManager()时;FragmentTransaction ft=fm.beginTransaction();ft.replace(R.id.fragment1,一,“fragmentone”).commit();ft.replace(R.id.fragment2,2,“fragment2”).commit();然后应用程序崩溃了。为什么?我不明白你的意思…………如果你使用的是v4版本,什么是片段事务版本检查?还要发布崩溃日志。没有日志就不容易知道崩溃原因。。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<fragment
android:id="@+id/fragment1"
android:name="com.example.Fragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment1"
android:layout_weight="1" />
<fragment
android:id="@+id/fragment2"
android:name="com.example.Fragment2"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment1"
android:layout_weight="1" />
</LinearLayout>