简体   繁体   中英

What is the best way to create this Layout in Android?

I have been struggling for hours to get this layout to work.

在此处输入图片说明

Here is my code:

<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>

Every fragment is a simple RelativeLayout (all have the same view):

<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>

Now I want make it work like this:

  • 1) No nested layout_weight

  • 2) No Nesting at all (for example nest the 2 first fragments etc etc)

  • 3) Not using code to do it programmatically after the view has rendered.

In my opinion the cleanest most readable way of doing this, would be to set the orientation of fragment 1 and fragment 2 to horizontal, and fragment 3 to vertical, but it does not work.

I've also checked this answer , but the guy, uses a layout_weight with a RelativeLayout. RelativeLayouts do ignore weights this will not work.

Any help will be appreciated?

You can add a dummy view of 0dp width and height in the center of the layout using a RelativeLayout , and position your Fragments accordingly:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM