我可以做一个Android按钮模板? [英] Can I make an Android button template?
问题描述
我在目前的应用程序几个按钮。他们除了他们的文字和标签都相同。在main.xml中会有很多更好,如果我没有重复所有的按钮配置信息的每一个按钮。
I have several buttons in my current app. They are all identical except for their text and a tag. The main.xml would be a lot nicer if I didn't have to repeat all the button config information for each button.
有没有一种方法来定义一个按钮为模板,然后让更多的使用它作为一个模板?
Is there a way to define a button as template, and then make more using it as a template?
在这个例子中,我有大约10以下的:
In this example, I have about 10 of the following:
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Y"
android:typeface="monospace"
android:textSize="12pt"
android:tag="Y"
android:textColor="@color/button_text"
android:background="@drawable/grey_blank_48x48"
android:onClick="onButtonClicked"/>
这将是很好,如果我有一个,然后这9:
It would be nice if I had one, and then 9 of these:
<Button2
android:text="N"
android:tag="N"/>
分辨率
据pretty的多@Luksprog说:
It was pretty much as @Luksprog said:
<style name="ASR33_button" >
<item name="android:id">@+id/button</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:padding">10dp</item>
<item name="android:textColor">@color/button_text</item>
<item name="android:background">@drawable/grey_blank_48x48</item>
<item name="android:onClick">onButtonClicked</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">12pt</item>
</style>
具有这样的事情在main.xml中:
with the main.xml having things like this:
<Button
style="@style/ASR33_button"
android:tag="Y"
android:text="Y"
/>
<Button
style="@style/ASR33_button"
android:tag="N"
android:text="N"
/>
这承载着日子。
推荐答案
我们一个 风格
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="btnlook" >
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="@color/button_text"
android:background="@drawable/grey_blank_48x48"
android:onClick="onButtonClicked"
android:typeface="monospace"
android:textSize="12pt"
</style>
</resources>
,然后在你的XML布局:
and then in your xml layout:
<Button
style="@style/btnlook"
android:text="N"
android:tag="N"/>
这篇关于我可以做一个Android按钮模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!