How to get RelativeLayout working with merge and include?

前端 未结 8 1940
余生分开走
余生分开走 2020-12-02 04:37

I have been trying for a few days now to make my layouts more efficient by converting from using several levels of nested LinearLayouts to one RelativeLay

相关标签:
8条回答
  • 2020-12-02 05:21

    Man, this is old, but it seems to come up at the top of searches, so I'm going to comment.

    I think the trick here is that the <merge> tag combined with the <include> tag essentially remove any sort of "parent" view group at that level. So then, who exactly are you asking to "layout_below" someone else? No one. There is no view at that level.

    The <merge> tag takes the child views and pops them right into the parent of the <include> tag. You must therefore ask the children in the layout you're including to anchor themselves accordingly.

    0 讨论(0)
  • 2020-12-02 05:21

    The android:layout_* attributes seem to be ignored when used in the include tag and all of the merged layouts are displayed on top of each other.

    My guess is that you cannot reference, from layout rules, android:id attributes that are defined on <include> elements, only ones that are on "real" widgets and containers.

    Also, if anyone can think of a way for me to just have one merge xml layout file instead of 5 that would be greatly appreciated.

    Simple: put them all in one file.

    I couldn't find a way to do that because I need to have access to each item in the merge layout files at runtime

    Whether you have one <include> element or 1,000, all of the contents should be accessible at runtime. One exception is if you have duplicated android:id attributes -- you would need to properly scope your findViewById() calls to get the right one, just like you have to when getting widgets from a ListView row.

    If you can create a sample project that uses 2+ merge files where you can demonstrate that the contents are not accessible at runtime, let me know.

    0 讨论(0)
  • 2020-12-02 05:21

    I had the same problem and even defining layout_width and layout_height it didn't work. The problem was that the layout I was including had tags and after removing them, everything worked like a charm. I suppose that merge is not a layout tag and because of that, it cannot receive positioning and size parameters. Since everything you define in is transferred to the inner parent layout, the settings just were throw away.

    TL:DR: Just remove the tags, move the xmlns definitions to a real layout viewholder and you should be good.

    Before:

    <merge
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <...ui.component.BorderCardView
            android:layout_width="112dp"
            android:layout_height="32dp"
            app:cardCornerRadius="4dp"
            app:cardUseCompatPadding="true">
    
            <ImageView
                android:layout_width="16dp"
                android:layout_height="16dp"
                android:src="@drawable/ic_logout"
                android:tint="@color/divider" />
    
        </...ui.component.BorderCardView>
    </merge>
    

    Working:

    <...ui.component.BorderCardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="112dp"
        android:layout_height="32dp"
        app:cardCornerRadius="4dp"
        app:cardUseCompatPadding="true">
    
        <ImageView
            android:layout_width="16dp"
            android:layout_height="16dp"
            android:src="@drawable/ic_logout"
            android:tint="@color/divider" />
    
    </...ui.component.BorderCardView>
    
    0 讨论(0)
  • 2020-12-02 05:30

    For positioning to work on RelativeLayout you need to set the layout_* parameters in the include file, not in the main layout file. That way

    main_layout.xml

    <RelativeLayout
      android:id="@+id/header"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">
       ....
    </RelativeLayout>
    
    <RelativeLayout 
      android:id="@+id/footer"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true">
        .....
    </RelativeLayout>
    
    <include layout="@layout/content_layout" />
    

    content_layout.xml

    <merge xmlns:android="http://schemas.android.com/apk/res/android">
    <RelativeLayout
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/footer"
        android:layout_below="@id/header" >
    
        ....
    </RelativeLayout>
    </merge>
    

    This is obviously not what us developers want, but it's the only solution I've found to avoid duplicating xml

    0 讨论(0)
  • 2020-12-02 05:41

    See the more highly voted answer below. Mine is woefully outdated


    i can address one issue Justin raised: inability of RelativeLayout to manage positioning of an include (at least in this simple case, on a 1.6 emulator)

    CommonsWare suggests wrapping the includes in a unique parent container, but does so in order to assist addressing & scoping identically named Views within Justin's includes

    Each would have to have a unique parent container, and you would call findViewById() on that container (ViewGroup) rather than on the Activity.

    In fact, you also must do it in order to get RelativeLayout to behave as expected:

    This works (footer is well positioned):

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <include android:id="@+id/header" layout="@layout/header"
            android:layout_alignParentTop="true" />
        <WebView android:id="@+id/webView" android:layout_below="@id/header"
            android:background="#77CC0000" android:layout_height="wrap_content"
            android:layout_width="fill_parent" android:focusable="false" />
        <LinearLayout android:layout_alignParentBottom="true"
            android:layout_height="wrap_content" android:layout_width="fill_parent">
            <include android:id="@+id/footer" layout="@layout/footer" />
        </LinearLayout>
    </RelativeLayout>
    

    This does not (footer is floating at top of screen):

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent">
        <include android:id="@+id/header" layout="@layout/header"
            android:layout_alignParentTop="true" />
        <WebView android:id="@+id/webView" android:layout_below="@id/header"
            android:background="#77CC0000" android:layout_height="wrap_content"
            android:layout_width="fill_parent" android:focusable="false" />
        <include android:id="@+id/footer" layout="@layout/footer"
            android:layout_alignParentBottom="true" />
    </RelativeLayout>
    

    The bare footer include will not align to bottom of parent, without the surrounding LinearLayout.. I wouldn't call this expected behavior.

    Additionally, the WebView appears to attach itself nicely to the header by ID, but I believe this to be illusion, due to it simply flowing below the header vertically. I also tried to set a button right above the footer include, but it got all floaty and wrong, too

    RelativeLayout had more problems in 1.5, but i still like it :)

    0 讨论(0)
  • 2020-12-02 05:41

    try :

    <RelativeLayout xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"
        tools:showIn="@layout/activity_home">
    
    0 讨论(0)
提交回复
热议问题