简体   繁体   中英

How to style the child view of a LinearLayout from the LinearLayout's own style?

Sorry for the possibly confusing title

So I'm using ViewPagerIndicator, which is a library commonly used for tabs before TabLayout was released in 5.0. In this library, tabs are views that extend TextView, that accepted a custom attribute for styling.

//An inner class of TabPageLayout
private class TabView extends TextView {
    private int mIndex;

    public TabView(Context context) {
        super(context, null, R.attr.vpiTabPageIndicatorStyle); //<--custom attribute
    }
    // ...
}

vpi__attrs.xml

<resources>
    <declare-styleable name="ViewPagerIndicator">
        ...
        <!-- Style of the tab indicator's tabs. -->
        <attr name="vpiTabPageIndicatorStyle" format="reference"/>
    </declare-styleable>
    ...

With this setup, when I used TabPageLayout in my own project, I could define the style of the text like this

<!--This is styles.xml of my project -->
<style name="MyStyle.Tabs" parent="MyStyle" >
        <item name="vpiTabPageIndicatorStyle">@style/CustomTabPageIndicator</item>

    </style>

    <style name="CustomTabPageIndicator">
        <item name="android:gravity">center</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textSize">@dimen/secondary_text_size</item>
         ...
    </style>

The following style would be applied to the Activity, and it would override the default vpiTabPageIndicator in the ViewPagerIndicator library.

My problem now is that I needed to make more customization to TabView than a TextView would allow, so I created a new inner class called "TabLayoutWithIcon" that extends LinearLayout and includes a TextView.

private class TabViewWithIcon extends LinearLayout {
    private int mIndex;
    private TextView mText;

    public TabViewWithIcon(Context context) {
        super(context, null, R.attr.vpiTabPageIndicatorStyle);
        //setBackgroundResource(R.drawable.vpi__tab_indicator);
        mText = new TextView(context);
    }
    ...

    public void setText(CharSequence text){
        mText.setText(Integer.toString(mIndex) + " tab");
        addView(mText);
    }

    public void setImage(int iconResId){
        mText.setCompoundDrawablesWithIntrinsicBounds(iconResId, 0, 0, 0);
        mText.setCompoundDrawablePadding(8); //Just temporary
    }

Now the same custom style is being applied to a LinearLayout, but I also want to style the child TextView. How can I do this?

Of course, I could also just pass in a style for the TextView programatically inside TabViewWithIcon,like

       mText.setTextAppearance(context, R.style.CustomTabTextStyle);

but then I would have to write my custom style inside the library, which I shouldn't be doing.

Do I need to redefine some attributes or something? Am I approaching this incorrectly?

Im an idiot, I just have pass the custom TextView style into the TextView

    public TabView(Context context) {
        super(context, null, R.attr.vpiTabPageIndicatorStyle);
        //setBackgroundResource(R.drawable.vpi__tab_indicator);
        mText = new TextView(context, null, R.attr.vpiTabPageIndicatorStyle);
    }

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