0

After a lot search I found a great article that describes how to make compound objects. The recipe works great, Eclipse shows it in the custom made widgets. Fantastic.

http://mobile.cs.fsu.edu/creating-a-simple-compound-component-in-android/

Just one problem, I need a few of them. When I added a few into a layout, the resource id for the enclosing layer/object bumps as expected; that is, the new object receives new ids auomagically. The issue is the components inside the main layout do not; so if I need to use finViewById to get the view for internal components, they all have the same id. Does anyone know how to automagically assign different id for the internal components of the complex widget? I would try to assign them manually, but Eclipse does not show the internal structure of a compound object; so no help there.

-- in response to Xaver answer

Yes, at least I think I did. If I follow the example in the link I have something alike; now I see what you are saying, I got the views as just by adding findviewbyId to custom class, as this

public class HorizontalNumberPicker extends LinearLayout {
    public HorizontalNumberPicker(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater layoutInflater = (LayoutInflater)context
                  .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layoutInflater.inflate(R.layout.horizontal_number_picker, this);

    View v = findViewbyID(R.id.btn_minus); // 
    }
}

What I cannot understand and I most likely have wrong is how to hook my code to two objects, that is following the example in the link

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent" >

     <example.example.HorizontalNumberPicker
         android:id ="@+id/horizontal_number_picker"
         android:layout_width ="wrap_content"
         android:layout_height ="wrap_content" />

     <example.example.HorizontalNumberPicker2
         android:id ="@+id/horizontal_number_picker"
         android:layout_width ="wrap_content"
         android:layout_height ="wrap_content" />

</FrameLayout>

How does HorizontalNumberPicker construtor know is linked to object horizontal_number_picker or horizontal_number_picker2?

if in in my activity I add

HorizontalNumberPicker obj1 = HorizontalNumberPicker (this,null);

what links to horizontal_number_picker or horizontal_number_picker2?

By the way how do I pass the Attributeset?

4

0 回答 0