[英]Android: inheritance of components in xml
I have 5 buttons on my activity that differ each other only with value height
: 我的活动中有5个按钮,只有值
height
相互不同:
<Button
android:id="@+id/someName"
android:layout_width="170dp"
android:layout_height="70dp"
android:layout_marginTop="10dp"
android:text="@string/some_text" />
... other buttons...
Is it possible to extends 4 buttons from first one and change only one of their property? 是否可以从第一个按钮扩展4个按钮并仅更改其中一个属性?
I know it is possible for styles, but what with components? 我知道它有可能是样式,但是组件是什么?
Or maybe should i do this in java code, but then how to connect to specific class? 或者也许我应该在java代码中这样做,但那么如何连接到特定的类?
I've found a detailed answer: 我找到了一个详细的答案:
http://docs.xamarin.com/recipes/android/resources/general/style_a_button/ http://docs.xamarin.com/recipes/android/resources/general/style_a_button/
My own example based on URL above: 我自己的例子基于上面的URL:
1) res\\drawable\\category_button.xml 1)res \\ drawable \\ category_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><shape android:shape="rectangle">
<solid android:color="#ef4444" />
<corners android:radius="15dp" />
</shape></item>
<item><shape android:shape="rectangle">
<solid android:color="#D8D8D8" />
<corners android:radius="15dp" />
</shape></item>
</selector>
2) res\\layout\\some_activity.xml 2)res \\ layout \\ some_activity.xml
... some xml code ...
<Button
android:id="@+id/buttonTestCatA"
style="@style/category_button"
android:background="@drawable/category_button"
android:text="@string/test_cat_A" />
... some xml code ...
3) res\\values\\styles.xml 3)res \\ values \\ styles.xml
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light"></style>
<style name="AppTheme" parent="AppBaseTheme"></style>
<style name="category_button">
<item name="android:layout_width">170dp</item>
<item name="android:layout_height">70dp</item>
<item name="android:layout_margin">5dp</item>
<item name="android:textSize">25sp</item>
</style>
</resources>
You must create a new XML
file in your drawable
folder. 您必须在
drawable
文件夹中创建一个新的XML
文件。 For example, let's rename it myCustomButton.xml
. 例如,让我们将其重命名为
myCustomButton.xml
。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:width="100dp"
android:height="20dp" />
</selector>
Then, for each button you want to get the same behaviour, add this line: android:background="@drawable/myCustomButton".
然后,对于每个想要获得相同行为的按钮,添加以下行:
android:background="@drawable/myCustomButton".
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.