Android: 更改方向时更改列表视图项目布局?
Android: change listview item layout when changing orientation?
如果我以横向模式启动应用程序,那么它将使用横向列表项 xml。但是如果我以纵向模式启动它并将其旋转到横向模式,它将对两种配置使用纵向列表项 xml。我正在使用 onConfigurationChanged 来检测方向变化,但我不确定如何告诉它使用正确的 xml 文件。我有两个同名的 xml 文件,但一个是横向的。谢谢。
如果你想实现这一点,你应该有 2 个文件夹 用于布局,一个应该被称为
layout-land/*
其他
layout-port/*
为您的布局命名相同
File.xml
不只是给它们充气 OS 照顾休息。
另见
Android layout folders: layout, layout-port, layout-land
specified android:configChanges="orientation" in the
AndroidManifest.xml. In that case specify again setContentView() in
the onConfigurationChanged() or remove that AndroidManifest.xml
directive
如果你只想继续使用 onconfigurationchanged() 而不是适当地膨胀。
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
如果我以横向模式启动应用程序,那么它将使用横向列表项 xml。但是如果我以纵向模式启动它并将其旋转到横向模式,它将对两种配置使用纵向列表项 xml。我正在使用 onConfigurationChanged 来检测方向变化,但我不确定如何告诉它使用正确的 xml 文件。我有两个同名的 xml 文件,但一个是横向的。谢谢。
如果你想实现这一点,你应该有 2 个文件夹 用于布局,一个应该被称为
layout-land/*
其他
layout-port/*
为您的布局命名相同
File.xml
不只是给它们充气 OS 照顾休息。
另见 Android layout folders: layout, layout-port, layout-land
specified android:configChanges="orientation" in the AndroidManifest.xml. In that case specify again setContentView() in the onConfigurationChanged() or remove that AndroidManifest.xml directive
如果你只想继续使用 onconfigurationchanged() 而不是适当地膨胀。
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}