【发布时间】:2014-02-13 19:03:54
【问题描述】:
我有一个自定义视图组,其中包含几个简单的视图。如何设置我的视图组的样式,以便某些属性到达某些子元素?例如,在下面的示例中,我如何创建允许轻松更改文本大小、颜色等的样式。如果可能的话,我想在 CustomViewGroup 上的 xml 中设置样式。我可以在创建样式时指定一个 ID 以便特定元素获取它吗?
示例用法:
<com.example.CustomViewGroup
android:id="@+id/custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
视图组的 XML:
<TextView
android:id="@+id/valueTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/descriptionTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/valueTextView"
android:layout_alignLeft="@id/valueTextView"
android:layout_marginTop="-4dip"
android:layout_marginLeft="26dip"/>
</RelativeLayout>
如果此视图的样式始终相同,这不是问题,但我想在不同的情况下使用不同的样式,这样我的应用程序的某些区域可以更大,而在其他区域更小.
提前致谢!
【问题讨论】:
-
尝试为每个底层子视图制作一些自定义属性(格式参考),您将在
CustomViewGroup上使用这些属性。然后为这些属性分配适当的样式,并在CustomViewGroup的构造函数中将它们应用到正确的子级。 -
感谢@Luksprog,这让我走上了正确的道路。如果你想看看我是怎么做的,请参阅下面的答案
标签: android android-view android-styles