【发布时间】:2015-03-23 01:41:32
【问题描述】:
我有一个应用程序,设备根据您的方向切换片段。每一个在各自的java类中都有一个文本视图和一个inflater。应用程序运行良好,但不会膨胀片段。在片段方面,我是初学者。这是我的代码:
MainActivity.java
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Configuration configInfo = getResources().getConfiguration();
if(configInfo.orientation == Configuration.ORIENTATION_LANDSCAPE){
LandscapeFragment fragmentLandscape = new LandscapeFragment();
fragmentTransaction.replace(android.R.id.content, fragmentLandscape);
}else{
PortraitFragment fragmentPortrait = new PortraitFragment();
fragmentTransaction.replace(android.R.id.content, fragmentPortrait);
}
fragmentTransaction.commit();
}
}
activity_main.xml
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/landscape_fragment" />
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/portrait_fragment" />
</RelativeLayout>
LandscapeFragment.java
public class LandscapeFragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
System.out.println("Landscape Fragment Created");
View v = inflater.inflate(R.layout.landscape_fragment, container, false);
return v;
}
}
landscape_fragment.xml
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/landscape_text_view"
android:textSize="30sp" />
</RelativeLayout>
PortraitFragment.java
public class PortraitFragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
System.out.println("Portrait Fragment Created");
View v = inflater.inflate(R.layout.portrait_fragment, container, false);
return v;
}
}
portrait_fragment.xml
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">
<TextView android:text="@string/portrait_text_view" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="30sp"/>
</RelativeLayout>
【问题讨论】:
标签: java android xml android-fragments