我的手机应用程序有一个主视图和向右滑动时的高级功能视图的寻呼机,在横向模式下,它有一个略微修改的版本,以适应横向。
现在我必须制作平板电脑的用户界面,我想在一个屏幕上显示两个视图,我已经成功地显示了它们,但在横向模式下的平板电脑正在从布局区域而不是纵向布局中拾取碎片,我需要它来挑选纵向的碎片并排显示。
我如何让片段选择肖像版本,即使平板电脑在横向,也不会创建重复的布局?
我的片段看起来像这样
public class PremiumFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup premiumView = (ViewGroup) inflater.inflate(
R.layout.premium_features, container, false);
return premiumView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.d(TAG, "onView created ran");
Intent intent = new Intent("setup");
LocalBroadcastManager.getInstance(getBaseContext()).sendBroadcast(intent);
}
}
发布于 2017-09-08 15:20:51
已经有一段时间了,但对于任何对这件事感兴趣的人来说:
如果我理解正确的话,你需要有一个纵向布局,一个横向布局(例如移动设备)和一个与纵向布局相匹配的不同的平板电脑横向布局。这里面没有那么多的欺骗。您可以在合格的布局中包含独立的非限定布局。
假设你有这些布局:
layout-land/my_fragment.xml
layout-port/my_fragment.xml
layout-large-land/my_fragment.xml
layout/my_fragment_content.xml
layout-port/my_Frament.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/my_fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout >
layout-land/my_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
//something completely different
</FrameLayout >
和layout-large-land/my_Frament.xml(内容同纵向):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/my_fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout >
其中涉及的重复性最小,但唯一重要的是,您没有理由接触layout-port/my_fragment.xml
或layout-large-land/my_fragment.xml
,因为所有重要信息都包含在单个文件layout/my_fragment_content.xml
中。
在这篇作者可能已经读过的文章中,对这个主题进行了更彻底的分析:
https://stackoverflow.com/questions/22074276
复制