[英]How to style the child view of a LinearLayout from the LinearLayout's own style?
抱歉,標題可能令人困惑
因此,我使用的是ViewPagerIndicator,這是在5.0版TabLayout發行之前通常用於標簽頁的庫。 在此庫中,選項卡是擴展TextView的視圖,這些視圖接受樣式的自定義屬性。
//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>
...
通過此設置,當我在自己的項目中使用TabPageLayout時,可以像這樣定義文本樣式
<!--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>
以下樣式將應用於Activity,它將覆蓋ViewPagerIndicator庫中的默認vpiTabPageIndicator。
我現在的問題是,我需要對TabView進行更多的自定義,而不是TextView所允許的,因此我創建了一個名為“ TabLayoutWithIcon”的新內部類,該類擴展了LinearLayout並包括一個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
}
現在,將相同的自定義樣式應用於LinearLayout,但是我還想為子TextView設置樣式。 我怎樣才能做到這一點?
當然,我也可以在TabViewWithIcon內以編程方式為TextView傳遞樣式,例如
mText.setTextAppearance(context, R.style.CustomTabTextStyle);
但是我不得不在庫中編寫自定義樣式,這是我不應該做的。
我需要重新定義一些屬性或某些東西嗎? 我處理方法有誤嗎?
我是個白痴,我只是將自定義TextView樣式傳遞給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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.