有一件事真的让我发疯。屏幕布局未正确地从陆地变为纵向。
我的布局文件夹中有两个文件夹来处理其中的方向和 .xml 文件,如下所示:
layout\main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:id="@+id/menu"
android:name="br.trebew.odonto.MenuData"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/princ"
android:name="br.trebew.odonto.FragList"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
和另一个:
layout-large-land\main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="290dp"
android:layout_height="fill_parent">
<fragment
android:id="@+id/menu"
android:name="br.trebew.odonto.MenuData"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/princ"
android:name="br.trebew.odonto.FragList"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<fragment
android:id="@+id/fdetalhe"
android:name="br.trebew.odonto.DetalheFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
我的主要 java 类是 OdontO.java,它是:
public class OdontO extends FragmentActivity implements OnDateChangeListener, OnAgendaItemClickListener {
FragList fList;
MenuData mData;
DetalheFragment frag_detalhe;
private boolean detalhesInLine;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("LayoutOr", "Entrou no onCreate OdontO");
mData = (MenuData) getSupportFragmentManager().findFragmentById(R.id.menu);
fList = (FragList) getSupportFragmentManager().findFragmentById(R.id.princ);
frag_detalhe = (DetalheFragment) getSupportFragmentManager().findFragmentById(R.id.fdetalhe);
detalhesInLine = (frag_detalhe != null &&
(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE));
if (detalhesInLine) {
fList.enablePersistentSelection();
}
else if (frag_detalhe != null) {
frag_detalhe.getView().setVisibility(View.GONE);
}
}
如果屏幕是纵向而不是旋转它会正确更改。
问题是相反的。如果屏幕是陆地,则旋转不会改变布局。事实上它正确调用了 onCreate 方法,但布局不会更改为纵向! =/
谁知道为什么?
最佳答案
问题可能是您的肖像位于普通尺寸屏幕的文件夹中,而您的风景位于大尺寸屏幕的文件夹中。也许您可以将 layout-large-land 更改为 layout-land(如果您的设备有普通屏幕)或将 layout 更改为 layout-large(如果您的设备有大屏幕)并查看它是否执行任何操作。 (注意:即使它有一个大屏幕,将它放在布局和 layout-land 中应该仍然有效)。
关于android - 屏幕方向和 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10213372/