您可以將樣式就像你可以做任何其他的看法,就像你表現出對您的問題。碰撞的原因可能是一個不同的原因。請記住,默認情況下,複合控件的元素不會將指定的樣式應用於控件本身。例如,如果您使用FrameLayout創建具有Button和EditText的複合控件,則設置複合控件的背景將嘗試應用於FrameLayout(控件的父控件),而不是內部元素(Button和EditText),除非你明確地確定。
如果您想爲您的組件添加自定義方法,您可以在attrs.xml
中這樣做。假設,例如,你想暴露的屬性修改組件的長寬比:
<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<declare-styleable name="MyCompoundComponent">
<attr name="aspectRatio" format="float"/>
</declare-styleable>
</resources>
然後在你的自定義控件的構造函數,你可以得到這些自定義道具的價值:
...
public MyCompoundComponent(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
if (attrs == null) return;
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyCompoundComponent, defStyleAttr, 0);
this.aspectRatio = typedArray.getFloat(R.styleable.MyCompoundComponent_aspectRatio, 1);
}
一旦出現,只要方便,您就可以簡單地使用所收集屬性的值。
真的,沒有答案? – SQLiteNoob 2014-08-05 18:24:54
不,至少兩年前,當我需要它時,我沒有找到解決方案。 – 2014-08-06 11:29:24
感謝您的回覆。當我找到解決方案時,我會記住這篇文章。 – SQLiteNoob 2014-08-07 15:24:50