简体   繁体   English

向android无边框按钮添加样式(android 4.0+)

[英]adding style to a android borderless button (android 4.0+)

I want to add a border to a borderlessbutton. 我想为无边框按钮添加边框。 However, if I create my own style with the parent borderlessbutton and I overwrite the background tag I loose the state change animation etc, as this is also defined by the background. 但是,如果我用父的borderlessbutton创建自己的样式并且覆盖了背景标签,则会丢失状态更改动画等,因为这也是由背景定义的。 I also cannot implement them myself as the native android drawables are not available as public and therefore not accessible. 我也无法自己实现它们,因为本地android可绘制对象无法作为公共对象使用,因此无法访问。 I do not want to have to copy the drawables. 我不想复制绘图。 Is it only possible to overwrite ondraw progammatically or is there an xml based solution i am missing? 是否只能以ondraw方式覆盖ondraw或我缺少基于xml的解决方案? (btw this is for a periodic table so this should not involve having an xml file for each button as there are about 100 of them) thanks stephan (顺便说一下,这是用于元素周期表的,因此这不应该涉及每个按钮都有一个xml文件,因为其中大约有100个按钮)

The bulk of this comes from this article . 这大部分来自本文

There is a 4 step process to doing something like this: 做这样的事情有四个步骤:

  1. Create an XML file that contains the states. 创建一个包含状态的XML文件。
  2. Create an XML file (Or background) for each state 为每个状态创建一个XML文件(或背景)
  3. Create the style of the button 创建按钮的样式
  4. Add the button to your layout, and see what it looks like. 将按钮添加到您的布局,然后看它的外观。

The single most difficult thing is the first step, so I'll show that one here. 唯一最困难的事情是第一步,因此我将在这里展示。 For the other ones, you can visit the site or do your own thing. 对于其他网站,您可以访问该网站或自己做。 Essentially, it will look something like this. 本质上,它将看起来像这样。 Basically needs to capture all of the 4 states. 基本上需要捕获所有4个状态。 This should be saved in the drawable folder, and the name of this is what your application will use for the name of the drawable. 这应该保存在drawable文件夹中,其名称就是您的应用程序将其用作drawable的名称。

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_enabled="false"
        android:drawable="@drawable/loc_for_button_disabled" />
    <item
        android:state_pressed="true"
        android:state_enabled="true"
        android:drawable="@drawable/loc_for_button_pressed" />
    <item
        android:state_focused="true"
        android:state_enabled="true"
        android:drawable="@drawable/loc_for_button_focused" />
    <item
        android:state_enabled="true"
        android:drawable="@drawable/loc_for_button_enabled" />
</selector>

An easy way would be to place your button within a separate layout, eg a LinearLayout and give this layout a background color and a padding of eg 1dp. 一种简单的方法是将按钮放置在单独的布局(例如LinearLayout并为该布局提供背景色和填充(例如1dp)。 This would render a "border" around the button. 这将在按钮周围呈现“边框”。 Note, that this is quite costly in regard of layouting, so do not use this method when you have lots of buttons. 请注意,就布局而言,这是相当昂贵的,因此,当您有很多按钮时,请勿使用此方法。

The correct solution would be to create your one drawables for all states and build a statelist drawable with your drawables and assign this statelist drawable as your button's background. 正确的解决方案是为所有状态创建一个可绘制对象,并使用可绘制对象构建一个可绘制状态列表,并将该可绘制状态列表指定为按钮的背景。 Actually it's not that much work, just have a look at http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList 实际上,它工作量不大,只需看一下http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM