简体   繁体   中英

How to use <merge> tag programmatically in java class without using xml layout in android?

I am trying to create this layout in android without using xml.

<merge
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" >

    <com.lorentzos.flingswipe.SwipeFlingAdapterView
        android:id="@+id/frame"
        android:background="#ffeee9e2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:rotation_degrees="15.5"
        tools:context=".MyActivity" />

    <include layout="@layout/buttons" />

</merge>

But I did not find merge tag in android code. So, I used Relative Layout as main parent layout which is as follows:

 private RelativeLayout mainActivityLayout;

 mainActivityLayout = new RelativeLayout(this);

    RelativeLayout.LayoutParams relativeLayoutMainParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT
    );

    SwipeFlingAdapterView flingContainer = new SwipeFlingAdapterView(this);
    flingContainer.setId(Constants.generateViewId());
    flingContainer.setBackgroundColor(Color.parseColor("#ffeee9e2"));
    //flingContainer.setRotation((float)15.5);
    RelativeLayout.LayoutParams flingContainerLayoutParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT
    );

    mainActivityLayout.addView(flingContainer, flingContainerLayoutParams);
    setContentView(mainActivityLayout, relativeLayoutMainParams);

Implementing this works, but is there any better way of using merge in android code?

Also if I need to add another view in this mainActivityLayout, calling addView() and providing params works. Is adding reusable view using addView() in class is same as using tag in xml?

<merge> is not something you use programatically - it's not a View . It's a compile-time attribute that lets the tools know to - well - merge the given layouts into any other layout that includes them.

See the documentation for more: https://developer.android.com/training/improving-layouts/reusing-layouts.html#Merge

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