我有2个片段:ls_fragment
和pm_fragment
。我试图在纵向模式下显示pm_fragment
,在横向模式下显示两个片段,但我的代码也不能正常工作。在纵向模式下,它可以显示pm_fragment
,但在横向模式下,它只显示一个片段(lm_fragment
)。
MainActivity代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
Configuration config = getResources().getConfiguration();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
LM_Fragment lm_fragment = new LM_Fragment();
PM_Fragment pm_fragment = new PM_Fragment();
/**
* Check the device orientation and act accordingly
*/
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
/**
* Landscape mode of the device
*/
fragmentTransaction.replace(android.R.id.content, lm_fragment);
fragmentTransaction.add(android.R.id.content, pm_fragment);
//fragmentTransaction.replace(android.R.id.content, pm_fragment);
}else{
/**
* Portrait mode of the device
*/
fragmentTransaction.replace(android.R.id.content, pm_fragment);
}
fragmentTransaction.commit();
}
}
和LM_Fragment
类(看起来像PM_Fragment
类):
public class LM_Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
/**
* Inflate the layout for this fragment
*/
return inflater.inflate(
R.layout.lm_fragment, container, false);
}
}
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<framelayout
android:name="com.example.myfragments.LM_Fragment"
android:id="@+id/lm_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<framelayout
android:name="com.example.myfragments.PM_Fragment"
android:id="@+id/pm_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
lm_fragment.xml(看起来像pm_fragment.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#7bae16">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/landscape_message"
android:textColor="#000000"
android:textSize="20px" />
<!-- More GUI components go here -->
</LinearLayout>
如何在风景中显示两个片段?
答案 0 :(得分:0)
尝试使用以下代码更改您的activity_main.xml。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:weightSum="2">
<framelayout
android:name="com.example.myfragments.LM_Fragment"
android:id="@+id/lm_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<framelayout
android:name="com.example.myfragments.PM_Fragment"
android:id="@+id/pm_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>