Anko如何更改按钮颜色?

时间:2018-10-21 19:04:03

标签: android-layout button android-button anko

我正在尝试更改LinearLayout中按钮的颜色:

class MoodActivityUI : AnkoComponent<MoodActivity> {
override fun createView(ui: AnkoContext<MoodActivity>) = with(ui) {
    frameLayout {
        verticalLayout {

            linearLayout {
                button("3"){
                    setBackgroundColor(Color.parseColor("red"))
                }.lparams{
                    width = dip(53)
                    height = dip(53)
                    leftMargin=dip(6)
                }

                button("2"){
                }.lparams{
                    width = dip(53)
                    height = dip(53)}

                button("1"){
                }.lparams{
                    width = dip(53)
                    height = dip(53)}
                ...

结果看起来像这样:

enter image description here

为什么它超出了按钮形状的边界? 我该如何更改按钮颜色?

1 个答案:

答案 0 :(得分:2)

您可以像这样使用“ themedButton”代替“ button”:

<div class="ce_image CollaboProfilePicture first block">
  <figure class="image_container" itemscope="" itemtype="http://schema.org/ImageObject" itemprop="associatedMedia">
    <img src="https://www.martin-missfeldt.de/images-pictures/grafik-illustration/bild-mit-wort-bild.jpg" alt="" itemprop="image" width="250" height="333">
  </figure>
</div>
<div class="ce_text CollaboPersonalDescription_1 block">
  <table style="border-collapse: collapse; width: x 100%;">
    <tbody>
      <tr style="height: 16px;">
        <td style="width: 15.0528%; height: 28px; padding-right: 60px;">Geburtsjahr:</td>
        <td style="width: 84.9472%; height: 28px;">2000</td>
      </tr>
      <tr style="height: 16px;">
        <td style="width: 15.0528%; height: 28px;">Lieblingsplatz:</td>
        <td style="width: 84.9472%; height: 28px;">Jumppark Zürich</td>
      </tr>
      <tr style="height: 16px;">
        <td style="width: 15.0528%; height: 28px;">Favorite trick:</td>
        <td style="width: 84.9472%; height: 28px;">Tuck No Hander</td>
      </tr>
      <tr style="height: 16px;">
        <td style="width: 15.0528%; height: 28px;">Bike Model:</td>
        <td style="width: 84.9472%; height: 28px;">Canyon Stiched 360</td>
      </tr>
      <tr style="height: 16px;">
        <td style="width: 15.0528%; height: 28px;">Hoodie:</td>
        <td style="width: 84.9472%; height: 28px;">GBSS Black</td>
      </tr>
    </tbody>
  </table>
  <p style="margin-top: 0px;">&nbsp;</p>
</div>
<!-- indexer::stop -->
<div class="mod_eventlist CollaboPersonalDescriptionNoFloat block">
  <h2>Upcoming Events</h2>
  <div class="event layout_upcoming upcoming even first last cal_4" itemscope="" itemtype="http://schema.org/Event">
    <time datetime="2018-10-24" class="date" itemprop="startDate">24.10.2018</time>
    <a href="https://www.zueritrails.ch/jumppark/" title="DirtContest Jumppark (Mittwoch, 24.10.2018)" itemprop="url">DirtContest Jumppark</a>
  </div>
</div>
<!-- indexer::continue -->
<div class="ce_hyperlink last block">
  <figure class="image_container" itemscope="" itemtype="http://schema.org/ImageObject">
    <a href="https://www.facebook.com/robin.bachofner.509" class="hyperlink_img" rel="" itemprop="contentUrl">
      <img src="assets/images/a/Instagram%20button_black-e3e5b7ad.png" alt="" itemprop="image" width="80" height="80">
    </a>
  </figure>
</div>

和styles.xml中的样式

themedButton("3", theme = R.style.MyButtonStyle) {
    }.lparams {
        width = dip(53)
        height = dip(53)
        leftMargin = dip(6)
    }