[英]What is the best way to create this Layout in Android?
我一直在努力几个小时才能使这种布局生效。
这是我的代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context=".MainPreviewActivity">
<fragment
android:name="com.apps.foo.CleanPreviewFragment"
android:id="@+id/clean_preview_fragment"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="match_parent">
</fragment>
<fragment
android:name="com.apps.foo.pointop.DirtyPreviewFragment"
android:id="@+id/filters_fragment"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="match_parent">
</fragment>
<fragment
android:name="com.apps.foo.pointop.ProcessedPreviewFragment"
android:id="@+id/processed_preview_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
</fragment>
</LinearLayout>
每个片段都是一个简单的RelativeLayout(都具有相同的视图):
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.apps.<whatever the fragment name is>">
</RelativeLayout>
现在我要使它像这样工作:
1)没有嵌套的layout_weight
2)根本没有嵌套(例如,嵌套两个第一个片段等)
3)渲染视图后,不使用代码以编程方式执行此操作。
在我看来,最干净,最易读的方法是将片段1和片段2的方向设置为水平,将片段3的方向设置为垂直,但它不起作用。
我也检查了这个答案 ,但那家伙使用了带有RelativeLayout的layout_weight
。 RelativeLayouts会忽略权重,这将不起作用。
任何帮助将不胜感激?
您可以使用RelativeLayout
在布局的中心添加宽度和高度为0dp
的虚拟视图,并相应地将Fragments定位:
<RelativeLayout
... >
<View
android:id="@+id/center_anchor"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent="true" />
<fragment
android:layout_toLeftOf="@id/center_anchor"
android:layout_above="@id/center_anchor"
... />
<fragment
android:toLeftOf="@id/center_anchor"
android:layout_below="@id/center_anchor"
... />
<fragment
android:toRightOf="@id/center_anchor"
... />
</RelativeLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.