Button highlight in android

后端 未结 5 1744
星月不相逢
星月不相逢 2021-02-08 23:59

I have the following code for a button in the layout file for my application

相关标签:
5条回答
  • 2021-02-09 00:00

    A button has a set of states that can be configured like this:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@color/your_color"
          android:state_pressed="true" />
        <item android:drawable="@color/your_color"
          android:state_focused="true" />
    </selector>
    

    You can create this as a file in your res/drawables folder and then use it in your button as the background. Supose that you called that file "my_button.xml" you can then use it like this:

    <Button
        android:background="@drawable/my_button"  
    

    Or like this:

    my_button.setBackgroundDrawable(getResources().getDrawable(R.drawable.my_button));
    

    Your color can be defined in the colors.xml in the res/values folder. If you don't have this file you can create it (android will recognize it). This is a good practise, but you can also replace your_color by #DC143C in the code above.

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="your_color">#DC143C</color>
    </resources>
    

    Note that this color is already set for crimson.

    You can also add an image for the background, replacing the "@color/your_color" by "@drawable/your_image".

    For more information you can follow this link in stackoverflow.

    0 讨论(0)
  • 2021-02-09 00:04

    What you need is selector, its a simple drawable file in which you can change colors for example of your button depending of its state, for example:

    <?xml version="1.0" encoding="utf-8"?>
    <selector
      xmlns:android="http://schemas.android.com/apk/res/android">
      <item 
        android:state_focused="true"
        android:drawable="@color/white"/>
       <item 
        android:state_pressed="true"
        android:drawable="@color/white"/>
       <item android:drawable="@color/red" />
    </selector>
    
    0 讨论(0)
  • 2021-02-09 00:17

    To get you to the correct resource here is the documentation of the StateList Drawable.
    check this link

    You just create and define it in your res/drawable subdirectory and set it as your buttons background.

    0 讨论(0)
  • 2021-02-09 00:21

    Create the following xml file in your drawable folder. And set your buttons background to this drawable.

    <?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
            <!-- Image display in background in select state -->
            <item android:drawable="@drawable/button_pressed" android:state_pressed="true"/>
    
            <!-- Image display in background in select state -->
            <item android:drawable="@drawable/button_pressed" android:state_focused="true"/>
    
            <!-- Default state -->
            <item android:drawable="@drawable/button"/>
    
        </selector>
    
    0 讨论(0)
  • 2021-02-09 00:26

    You can define the color on ALL of your controls like this

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Some attributes like windowNoTitle, windowActionBar, ... -->
            <item name="colorControlHighlight">@color/colorAccent</item> <!-- chose the color here -->
    </style>
    
    0 讨论(0)
提交回复
热议问题