Android:xml中组件的继承
我的“活动”中有5个按钮,它们仅在值Android:xml中组件的继承,android,xml,android-layout,button,inheritance,Android,Xml,Android Layout,Button,Inheritance,我的“活动”中有5个按钮,它们仅在值高度上有所不同: <Button android:id="@+id/someName" android:layout_width="170dp" android:layout_height="70dp" android:layout_marginTop="10dp" android:text="@string/some_text" /> ... other buttons... ...
高度上有所不同:
<Button
android:id="@+id/someName"
android:layout_width="170dp"
android:layout_height="70dp"
android:layout_marginTop="10dp"
android:text="@string/some_text" />
... other buttons...
... 其他按钮。。。
是否可以从第一个按钮扩展4个按钮,并仅更改其中一个属性?
我知道样式是可能的,但是组件呢?
或者我应该在java代码中这样做,但是如何连接到特定的类呢?
您必须在drawable
文件夹中创建一个新的XML
文件。例如,让我们将其重命名为myCustomButton.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:width="100dp"
android:height="20dp" />
</selector>
然后,对于每个想要获得相同行为的按钮,添加以下行:android:background=“@drawable/myCustomButton”。我找到了一个详细的答案:
我自己的例子基于上面的URL:
1) res\drawable\category\u 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\u 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 ...
。。。一些xml代码。。。
... 一些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>
170dp
70dp
5dp
25便士
据我所知,res文件夹中的所有名称应只包含小的lether,然后允许使用my_custom_button.xml。无论如何,这不起作用。编译器抱怨应该扩展的属性不可见。此解决方案比使用styles.xml好吗?请从链接中提取关键注释并将其添加到您的答案中,这样即使链接不存在也可以理解。你也可以接受自己的答案。