8

我正在根据这个例子开发一个应用程序。我在 layout-land 文件夹中为 header.xml 定义了横向布局,但是当我将方向更改为横向时,定义的布局不会出现在屏幕中。

知道为什么吗?

谢谢

更新 :

活动代码:

public class ACENewsFeedActivity extends ListActivity {

    // Progress Dialog
    private ProgressDialog pDialog;

    // Array list for list view
    ArrayList<HashMap<String, String>> rssItemList = new ArrayList<HashMap<String,String>>();

    RSSParser rssParser = new RSSParser();

    List<RSSItem> rssItems = new ArrayList<RSSItem>();

    RssFeed rssFeed;

    private static String TAG_TITLE = "title";
    private static String TAG_LINK = "link";
    private static String TAG_DESRIPTION = "description";
    private static String TAG_PUB_DATE = "pubDate";
    //private static String TAG_GUID = "guid"; // not used

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rss_item_list);

        /**
         * Calling a backgroung thread will loads recent articles of a website
         * @param rss url of website
         * */
        new loadRSSFeedItems().execute();
       }

       ....
}

横向模式下的 XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutHeader"
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    android:layout_alignParentTop="true"
    android:background="@layout/header_gradient"
    android:orientation="horizontal">

    <!-- Logo -->

    <!-- Refresh -->


    <!-- Plus Button -->

    <ImageButton
        android:id="@+id/btnAddSite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="5dip"
        android:background="@null"
        android:src="@drawable/plus"
        android:layout_centerVertical="true" />

    <ImageView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/logo" />

    <ImageView
        android:id="@+id/refreshList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/refresh" />

</RelativeLayout>
4

2 回答 2

20

Android 允许您提供不同版本的资源文件以支持特定的设备配置,包括屏幕尺寸/分辨率和(如您所愿)设备方向。当 android 加载布局文件时,它会首先在res/layout-port文件夹中查找(如果它是纵向的)或res/layout-land文件夹中(如果它是横向的)。如果找不到该文件,它将在常规res/layout文件夹中查找。

此外,正如这里所指出的,当某些设备配置在运行时发生变化(如设备方向)时,android 将通过保存状态、销毁它,然后使用保存的状态信息启动它来重新启动当前正在运行的任何进程。这允许它再次加载布局文件,当它尝试加载它们时,它将查找新方向的文件夹。

因此,如果您以纵向方式启动应用程序,它会将文件加载到res/layout-portres/layout. 如果您随后将设备旋转到横向,它将破坏您的进程并重新启动。但是,这一次它将是横向的,因此它将检查res/layout-land布局文件。

如果您以这种方式设置文件,但它没有按您认为的那样运行,我首先通过将两个不同的header.xml文件放在layout-landlayout-port文件夹中来验证它肯定没有使用正确的文件,也许一个带有红色背景和一个绿色背景。确保仔细检查文件引用,并可能使用 Toast 在屏幕上发布一些调试信息,以确保它正确地膨胀布局。

默认行为是让 android 处理方向更改(这涉及破坏您的活动并创建它的新实例,该实例将重新加载所有布局文件)。除非清单文件中的活动标签包含属性 android:configChanges="orientation",否则此默认行为将始终发生。(此标签可以接受除方向以外的参数 - android 将处理所有事件的配置更改,除了您作为参数传递给此标签的事件。)

如果您包含android:configChanges="orientation"标签,则告诉 android 不要破坏您的活动,并且不要在设备方向更改时重新加载布局文件。而不是它的默认行为,它将调用一个方法(您定义),以允许您进行任何您希望自己处理方向更改的更改,而不是让 android 自动处理它。这样做的目的是,如果销毁您的活动会带来很大的不便,则不必自动销毁它。

编辑:从评论讨论中添加了一些东西

于 2012-06-05T19:41:04.440 回答
5

您应该android:configChanges="orientation"在该活动的manifest文件中定义并覆盖其中的onConfigChanged()方法setContentView()

像这样:

@Override    
public void onConfigurationChanged(Configuration newConfig) {
    setContentView(R.layout.your_xml);

    super.onConfigurationChanged(newConfig);
}
于 2012-09-25T14:53:18.240 回答