Bab*_*loo 8 android android-layout
我正在根据这个例子开发一个应用程序.我在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();
}
....
}
Run Code Online (Sandbox Code Playgroud)
横向模式下的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>
Run Code Online (Sandbox Code Playgroud)
mat*_*784 20
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自动处理它.它的目的是,如果破坏您的活动将是一个很大的不便,它不必自动销毁.
编辑:在评论讨论中添加了一些内容
小智 5
您应该android:configChanges="orientation"在manifest文件中为该活动定义并覆盖该onConfigChanged()方法setContentView()
像这样:
@Override
public void onConfigurationChanged(Configuration newConfig) {
setContentView(R.layout.your_xml);
super.onConfigurationChanged(newConfig);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13944 次 |
| 最近记录: |