[英]defined xml layout in layout-land does not appear in android application
我正在根據這個例子開發一個應用程序。 我在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>
Android允許您提供不同版本的資源文件,以支持特定的設備配置,包括屏幕大小/分辨率和(當您嘗試這樣做時)設備方向。 當android加載一個布局文件時,它將首先在res/layout-port
文件夾中(如果它是縱向)或res/layout-land
文件夾(如果它是橫向)。 如果找不到該文件,則會查看常規res/layout
文件夾。
此外,如上所述這里 ,當某些設備配置的Android運行過程中改變(如設備方向)將重新啟動任何程序目前被保存狀態,摧毀它,然后用保存的狀態信息開始運行它。 這允許它再次加載布局文件,並在嘗試加載它時將其視為新方向的文件夾。
因此,如果您以縱向方式啟動應用程序,它將在res/layout-port
或res/layout
加載文件。 如果您隨后將設備旋轉到橫向,則會破壞您的進程並重新啟動。 但是,這次它將處於橫向狀態,因此它將檢查res/layout-land
而不是布局文件。
如果您在文件中設置了這種方式,但它不工作,你認為它應該,我會先驗證它是絕對不會使用正確的文件,通過將兩個不同的header.xml
文件的layout-land
和layout-port
文件夾,可能是一個紅色背景和一個綠色背景。 確保仔細檢查文件引用,並使用Toast在屏幕上發布一些調試信息,以確保它正確地擴展布局。
默認行為是android處理方向更改(這涉及銷毀您的活動並創建一個新實例,它將重新加載所有布局文件)。 除非清單文件中的活動標記包含屬性android:configChanges =“orientation”,否則將始終發生此默認行為。 (此標記可以采用除orientation之外的參數 - android將處理除您作為此標記的參數傳遞的所有事件的配置更改。)
如果你包含android:configChanges="orientation"
標簽你告訴android不要破壞你的活動,而不是在設備方向改變時重新加載布局文件。 它將調用一個方法(您定義)來代替其默認行為,允許您進行任何您希望自己處理方向更改的更改,而不是讓Android自動處理它。 它的目的是,如果破壞您的活動將是一個很大的不便,它不必自動銷毀。
編輯:在評論討論中添加了一些內容
你應該在該活動的manifest
文件中定義android:configChanges="orientation"
並覆蓋該setContentView()
的onConfigChanged()
方法
像這樣:
@Override
public void onConfigurationChanged(Configuration newConfig) {
setContentView(R.layout.your_xml);
super.onConfigurationChanged(newConfig);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.