简体   繁体   中英

Create a composite view in Android

I'd like to create a custom view (I'll call it MyComplexView ), for example a RelativeLayout with an Imageview , a TextView , and a Button .

I'd like to declare an xml with the layout and then create the class:

MyComplexView extends RelativeLayout{...}

But I don't know what I should override to indicate which layout should be inflated.

How can I do this? Thanks

Something like this:

  1. add the constructors from the super class. (the one with just context is for creating the views programaticaly, the others are for when you add the view in XML.

  2. create a method called init() for example and call it from each constructor.

  3. inside the init method do:

    LayoutInflater.from(context).inflate(R.layout.my_view_layout, this, true);

now in inflate the additional params actually mean:

true -> attach the layout to the root in your case relative layout (pro-tip: so inside the xml you can have just merge tags if your layout root is also relative layout and align them in code so the hierarchy is simpler) or any layout you like.

this -> the layout to attach the inflated view to in your case the relative layout you are extending.

it will automatically be attached to the root -> extends RelativeLayout.

then you can use findViewById like:

this.findViewById(R.id.myView);

I'm not 100% sure what your main goal is, so I try to be thorough:

If you want to include a complex layout in other layouts, then you can simply define my_complext_layout.xml , and in your other layouts put:

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

If you need to run your own code, then you could simply make the root of this layout to be MyComplexView , and you can run code when the view is created.

If you have intended to let your code operate on the layout, then simply implement an OnGlobalLayoutListener and add it to your layout in your views constructor.

Implement a Constructor for the MyComplexView :

public MyComplexView(Context context, AttributeSet attrs){
    LayoutInflater inflater = LayoutInflater.from(context);
    inflater.inflate(R.layout.header_view, this, true);
    mHeaderView = (TextView)findViewById(R.id.header);
    if(mHeaderView != null)
        mHeaderView.setText("Test");

}

See Custom Components in the developer docs. In particular the Compound Controls section.

Once you've made your java file, in order to refer to it in xml you'll have to use a fully qualified packagename ie:

<com.yourpackage.YourCustomView
android:layout_height="wrap_content"
android:layout_width="wrap_content" />

Creating a custom view usually is aimed to create a widget which doesn't exist yet. What you're trying to do is to have the same layout repeated at multiple places.

You have severall options to do that according to your context.

  1. If the layout is to be placed in a lest, just create your layout in a separate file, and use it in a ListAdapter. Take a look at the ListView Tutorial for this.

  2. If this layout is a generic layout to be embedded in multiple activities, try using a Fragment instead. Fragments are subparts of an activity, with their own views. Alternatively, you can just embed the layout in severall xml using the tag.

  3. If really you want a custom class and single widget, then you need to extend the View class. Extending a layout means you wan't to organize child widgets differently (for example, organize them in circle). Extending a View, you can have exactly what you want (button, image, text) organized always in the same way. But I won't lie to you, this will mean lot of work.

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