我有2个片段:ls_fragment
和pm_fragment
。
我试图在纵向模式下显示pm_fragment
,在横向模式下显示两个片段,但我的代码不起作用。在纵向模式下它可以工作,但在横向模式下它只显示一个片段。
MainActivity代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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);
}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="fill_parent"
android:layout_height="fill_parent"
android:background="#7bae16">
<TextView
android:layout_width="fill_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)
这可能会对你有帮助。
我刚刚更新了你的代码并且它有效。
import android.content.res.Configuration;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
/**
* Created by Ravi on 09/07/16.
*/
public class Test12 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test123);
Configuration config = getResources().getConfiguration();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
MyFrag1 ls_fragment = new Myfrag1();
Myfrag2 pm_fragment = new Myfrag2();
/**
* Check the device orientation and act accordingly
*/
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
/**
* Landscape mode of the device
*/
fragmentTransaction.replace(R.id.containerView1, ls_fragment);
fragmentTransaction.replace(R.id.containerView2, pm_fragment);
}else{
/**
* Portrait mode of the device
*/
fragmentTransaction.replace(R.id.containerView1, pm_fragment);
}
fragmentTransaction.commit();
}
}
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/containerView1"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:orientation="vertical"/>
<FrameLayout
android:id="@+id/containerView2"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:orientation="vertical"/>
</LinearLayout>
答案 1 :(得分:0)
您只需要在MainActivity
代码的下方删除。
fragmentTransaction.add(android.R.id.content, pm_fragment)
我猜您在活动类中放错了这段代码,因为当您检查设备的方向是横向时,您正在用android.R.id.content
替换lm_fragment
。