如何自动应用父级的样式?
我有一个包含自定义布局的布局。我想将样式从一个元素传递到另一个元素。我有一个类可以扩展布局,如果需要我可以发布它。该设计元素由两种不同的布局添加,根据需要具有不同的尺寸。
Child layout
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal">
<TableRow android:layout_height="wrap_content" android:gravity="center_horizontal" android:layout_width="fill_parent" android:id="@+id/tableRow1">
<Button android:text="1" android:id="@+id/nb1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="40sp" android:layout_margin="1dip" android:minWidth="70dip"></Button>
<Button android:text="2" android:id="@+id/nb2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="40sp" android:layout_margin="1dip" android:minWidth="70dip"></Button>
<Button android:text="3" android:id="@+id/nb3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="40sp" android:layout_margin="1dip" android:minWidth="70dip"></Button>
</TableRow>
</TableLayout>
这是父布局
Parent Layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<pearsonartphoto.AJEG.number_viewer android:id="@+id/numberviewer" style="@style/bigViewer" android:layout_alignParentRight="true" android:layout_alignParentBottom="true">
</pearsonartphoto.AJEG.number_viewer>
</RelativeLayout>
我想做的是传递一个样式,并让该样式在所有子元素中重复,或者至少在 textView 元素中重复。我需要做什么才能实现这一点?
I have a layout that contains a custom layout. I would like to pass the style from one element to another. I have a class which inflates the layout, which I can post if required. This element of design is added by 2 different layouts, with different sizes depending on what is required.
Child layout
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal">
<TableRow android:layout_height="wrap_content" android:gravity="center_horizontal" android:layout_width="fill_parent" android:id="@+id/tableRow1">
<Button android:text="1" android:id="@+id/nb1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="40sp" android:layout_margin="1dip" android:minWidth="70dip"></Button>
<Button android:text="2" android:id="@+id/nb2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="40sp" android:layout_margin="1dip" android:minWidth="70dip"></Button>
<Button android:text="3" android:id="@+id/nb3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="40sp" android:layout_margin="1dip" android:minWidth="70dip"></Button>
</TableRow>
</TableLayout>
And here's the parent layout
Parent Layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<pearsonartphoto.AJEG.number_viewer android:id="@+id/numberviewer" style="@style/bigViewer" android:layout_alignParentRight="true" android:layout_alignParentBottom="true">
</pearsonartphoto.AJEG.number_viewer>
</RelativeLayout>
What I would like to do is to pass in a style, and have the style be repeated through all of the children, or at least the textView elements. What do I need to do to make this happen?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,在android中没有办法将父视图的样式继承给子视图,而不仅仅是嵌套它们!
你可以做的就是将样式应用于父级
比方说 styleA,然后将样式应用于子元素,比方说 styleB
,继承可以在样式中完成,我的意思是 styleB 可以从 styleA 继承属性,并且还可以覆盖他的一些父值
填充父级
包装内容
#00FF00
等宽字体
填充父级
包装内容
#00FF00
等宽字体
坏消息是您必须在每个子元素中添加样式:-(
我认为这是为某些元素提供样式的唯一方法,如果您想为一组元素提供样式,那么您必须定义主题并将其应用到活动或应用程序,这通常是在代码中完成的,但它可以也可以通过编程方式完成。
As far as I know there is no way in android to inherit the style of parent to the children views, not by just nesting them !
the thing that you could to is to apply a style to the parent
let say styleA, and than apply a style to the children elements let say styleB
and the inheritance could be done in the styles, I mean styleB can inherit properties from styleA and also override some of his parent values
fill_parent
wrap_content
#00FF00
monospace
fill_parent
wrap_content
#00FF00
monospace
the bad news is that you must put style in every children element :-(
I think this is the only way to give a style to some element, if you want to give a style to group of elements than you must define theme and apply it to the activity or to the application this is done usually in code but it could be done pro programmaticlly also.