【问题标题】:Fragment layout not being inflated片段布局没有被夸大
【发布时间】: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


    【解决方案1】:

    当您需要在运行时更改布局时,您不应在活动的布局文件中声明片段。将您的 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">
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/main_content" />
    
    </RelativeLayout>
    

    并将您的片段创建代码更改为

        if(configInfo.orientation == Configuration.ORIENTATION_LANDSCAPE){
    
            LandscapeFragment fragmentLandscape = new LandscapeFragment();
    
            fragmentTransaction.replace(R.id.main_content, fragmentLandscape);
    
        }else{
    
            PortraitFragment fragmentPortrait = new PortraitFragment();
    
            fragmentTransaction.replace(R.id.main_content, fragmentPortrait);
    
        }
    

    文档here 将帮助您更多地了解片段

    【讨论】:

      【解决方案2】:

      不要在 MainActivity 中编写任何代码, 正如@jobcrazy 给出的链接,直接在 xml 中执行:

      <fragment
          android:name="yourpackage.PortraitFragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:id="@+id/landscape_fragment" />
      
      <fragment
         android:name="yourpackage.LandscapeFragment
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:id="@+id/portrait_fragment" />
      

      应该可以解决的!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-15
        • 1970-01-01
        • 1970-01-01
        • 2015-08-13
        • 2018-12-15
        相关资源
        最近更新 更多