Tytuł pytania prawdopodobnie jest bezsensowny. Tworzę kilka niestandardowych widoków, które zostaną umieszczone w układzie pojedynczego nadrzędnego - niestandardowego FrameLayout
.Uzyskaj atrybuty stylu dla dziecka z definicji stylu rodzica.
Te niestandardowe widoki mają własny styl attr, które są ustawiane przy użyciu atratu rodzica Attr.
Jako przykład należy wziąć pod uwagę, że Parent
jest niestandardowym FrameLayout
. Jego styl attr
jest zdefiniowana w attrs.xml
:
<attr name="parentStyleAttr" format="reference" />
Child
posiada również attr:
<attr name="childStyleAttr" format="reference" />
I Parent
definiuje jego styleable atr jak:
<declare-styleable name="Parent">
<attr name="childStyleAttr" />
</declare-styleable>
Child's
styleable attr:
<declare-styleable name="Child">
<attr name="childBgColor" format="color" />
</declare-styleable>
Po tym, ja zdefiniować styl dla rodzica:
<style name="ParentStyle">
<item name="childStyleAttr">@style/ChildStyle</item>
</style>
i jeden dla Child
:
<style name="ChildStyle">
<item name="childBgColor">@color/blah</item>
</style>
Dla Parent
, skonfigurować parentStyleAttr
w App tematem:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="parentStyleAttr">@style/ParentStyle</item>
</style>
Po utworzeniu Parent
nadmuchuje s a układ zawierający Child
:
LayoutInflater.from(getContext()).inflate(R.layout.child, this, true);
Podczas Child's
inicjalizacji, muszę odczytać wartość atrybutu stylu ustawioną w @style/ChildStyle
- childBgColor
.
To nie działa:
final TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.Child, R.attr.childStyleAttr, R.style.ChildStyle);
Sposób Jestem obecnie czytania attr/childBgColor
jest:
public Child(Context context, AttributeSet attrs, int defStyleAttr) {
super(createThemeWrapper(context), attrs, defStyleAttr);
initialize(attrs, defStyleAttr, R.style.ChildStyle);
}
private static ContextThemeWrapper createThemeWrapper(Context context) {
final TypedArray forParent = context.obtainStyledAttributes(
new int[]{ R.attr.parentStyleAttr });
int parentStyle = forParent.getResourceId(0, R.style.ParentStyle);
forParent.recycle();
TypedArray forChild = context.obtainStyledAttributes(parentStyle,
new int[]{ R.attr.childStyleAttr });
int childStyleId = forChild.getResourceId(0, R.style.ChildStyle);
forChild.recycle();
return new ContextThemeWrapper(context, childStyleId);
}
void initialize(AttributeSet attrs, int defStyleAttr, int defStyleRes) {
Context context = getContext();
final Resources res = getResources();
final TypedArray a = context.obtainStyledAttributes(R.styleable.Child);
....
}
nie jestem przekonany, czy to jest właściwe podejście. Czy ktoś może rzucić trochę światła na to?
Twój kod nie działa lub po prostu chcesz go poprawić? –
@NikMyers Chcę wiedzieć, czy moje obecne podejście jest poprawne. A jeśli tak nie jest, jakie jest właściwe podejście? – Vikram
Może się nie udać, ale problem, który opisujesz, brzmi jak dziedziczenie tematu nadrzędnego, które zostało zaimplementowane w najnowszej aktualizacji 'appcompat': https://chris.banes.me/2015/04/22/support-libraries -v22-1-0/# androidtheme Autor wymienił 'LayoutInflater.Factory2' jako klasę, która włącza dziedziczenie tematu nadrzędnego. – hidro