Ответ 1
Хотя идея приходит немного поздно, и метод не прямолинейный, я думаю, что он по-прежнему стоит поделиться. Мы можем поместить пользовательские атрибуты в тему, поэтому атрибуты могут быть переданы всем дочерним представлениям из родительского представления, в котором используется тема (т.е. Атрибуты существуют в группе представлений).
Пример следующим образом:
<integer name="percentage">35</integer>
<style name="CustomTheme" parent="suitable theme for your case">
<item name="percent">@integer/percentage</item>
</style>
<com.example.CustomLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:theme="@style/CustomTheme" >
<com.example.CustomView
android:id="@+id/customView1"
app:percent="?attr/percent" <!--Note: that how it refers to the value,
however, re-assign the attribute value here is meaningless as attribute percent
should exist in all child views now. You can retrieve its value via
Theme.obtainStyledAttributes(R.style.CustomTheme, new int[] {R.attr.percent})
in every child view-->
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</com.example.CustomLayout>