Can I make an Android button template?

前端 未结 4 2104
被撕碎了的回忆
被撕碎了的回忆 2021-02-09 00:32

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

相关标签:
4条回答
  • 2021-02-09 01:19

    Us a style:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <style name="btnlook" >
            <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>
    </resources>
    

    and then in your xml layout:

    <Button
                style="@style/btnlook"
                android:text="N"
                android:tag="N"/>
    
    0 讨论(0)
  • 2021-02-09 01:22

    The only way to do this to make custom button subclassing the button.

    0 讨论(0)
  • 2021-02-09 01:25

    You can make a custom view that extends button and sets all the things that is repeating. Then you can use it as you described, but with a fully qualified name, not just shortname.

    class MyButton extends Button {
        public MyButton() {
            // Set the values you want
        }
    }
    

    And in XML:

    <com.me.myapp.MyButton
        android:text="N"
        android:tag="N" />
    
    0 讨论(0)
  • 2021-02-09 01:38

    You can create a selector

    <?xml version="1.0" encoding="UTF-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" 
         android:shape="rectangle">
        <solid android:color="#AAFFFFFF"/>
        <corners android:bottomRightRadius="7dp"
            android:bottomLeftRadius="7dp" 
            android:topLeftRadius="7dp"
            android:topRightRadius="7dp"/>
    

    And set the 10 button android:background="@drawable/that_selecter"

    0 讨论(0)
提交回复
热议问题