سؤال

I have a custom view group that contains a couple simple views. How can I style my view group so that certain properties reach certain child elements? For instance, in the example below how can I create style that allows for easy change of text size, color, etc. I'd like to set the style in xml on the CustomViewGroup if at all possible. Can I specify an ID when creating styles so that specific elements get it?

Example usage:

<com.example.CustomViewGroup
    android:id="@+id/custom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

XML of the view group:

    <TextView
        android:id="@+id/valueTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView 
        android:id="@+id/descriptionTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/valueTextView"
        android:layout_alignLeft="@id/valueTextView"
        android:layout_marginTop="-4dip"
        android:layout_marginLeft="26dip"/>


</RelativeLayout>

If the style for this view was the same the whole time this wouldn't be an issue but I would like to use different styles in different situations so I can have it be bigger in some areas of my app but smaller in others.

Thanks in advance!

هل كانت مفيدة؟

المحلول

Based upon the comment from @Luksprog I was able to go down a path that led to a not-so-perfect but not-ugly-enough-that-I-hate-it solution.

Since I wasn't creating and adding the views in my ViewGroup constructor and was inflating from an xml file I couldn't just add attributes that reference styles (see this). So I ended up creating an enum attribute that switches between the various styles I could want for this view. Then in the constructor I inflated different layouts (where the styles were set in the xml) based upon this attribute.

Example:

    public CustomViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context, attrs, defStyleAttr);
    }

    private void initialize(Context context, AttributeSet attrs, int defStyleAttr) {
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomViewGroup, defStyleAttr, 0);

            int style = a.getInt(R.styleable.CustomViewGroup_styleType, 0);
            switch (style) {
                case 0:
                    View.inflate(context, R.layout.custom_view_group_small, this);
                    break;
                case 1:
                    View.inflate(context, R.layout.custom_view_group_large, this);
                    break;
                default:
                    View.inflate(context, R.layout.custom_view_group_large, this);
                    break;
            }

            ...
        }
    }

Again not the most elegant solution but its working for me and I can now update the styles pretty easily without having to change a bunch of files.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top