Android-管理布局景观 [英] Android - Manage layouts landscape

查看:55
本文介绍了Android-管理布局景观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一项活动,根据方向加载不同的片段:

I need an activity load different fragments depending orientation:

活动->(人像)片段肖像

Activity-> (portrait) Fragment Portrait

活动->(风景)片段风景

Activity-> (landscape) Fragment Landscape

这是activity_layout.xml

this is activity_layout.xml

<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"
tools:context="com.examples.MainActivity"
android:background="#FF0000">

<fragment
    android:id="@+id/navigationContainerFragment"
    android:name="com.examples.FragmentPort"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

 </LinearLayout>

这是activity_layout_land.xml

this is activity_layout_land.xml

 <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"
    tools:context="com.examples.MainActivity"
     android:background="#FF0000">


  <fragment
      android:id="@+id/navigationContainerFragment"
     android:name="com.examples.FragmentLandscape"
     android:layout_width="match_parent"
     android:layout_height="match_parent"/>

 </LinearLayout>

然后是fragmentPortrait.xml

Then fragmentPortrait.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent"
   android:layout_height="match_parent">

   <TextView
      android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:gravity="center"
       android:text="FRAGMENT PORTRAIT"/>

</LinearLayout>

和FragmentLanscape.xml

And FragmentLanscape.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical" android:layout_width="match_parent"
     android:layout_height="match_parent">

  <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:text="FRAGMENT LANDSCAPE"/>

 </LinearLayout>

问题是旋转时我看不到FragmentLandscape.有人能帮我吗?谢谢

Problem is that I do not see FragmentLandscape when rotating. Can someone help me? Thanks

更新:我已经在不同的文件夹中放置了不同的布局:/layout和/layout-land

UPDATE: I already put different layouts in different folders: /layout and /layout-land

推荐答案

res 中创建两个文件夹,名称为 layout-land layout-port

Create two folder in res with name layout-land or layout-port

将Portrait xml文件放置在 layout-port:

Place Portrait xml file in layout-port:

将景观xml文件放置在 layout-land:

Place landscape xml file in layout-land:

请记住,两个文件都具有相同的名称

Keep that in mind file both file will have same name

这篇关于Android-管理布局景观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆