【问题标题】:Add shadow to button in android在android中为按钮添加阴影
【发布时间】:2013-06-14 12:03:03
【问题描述】:

我正在尝试在单击时在图像按钮上添加某种阴影。是否可能以及如何。

所以我有按钮,我希望当我单击它时它看起来会被推动。所以我创建了这样的新xml:

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

好的,它工作正常,但我有 50 个按钮,我必须为所有按钮制作新图像。这是很多工作,是否可以在其上添加某种阴影?或者让它看起来像是被推动的东西?

【问题讨论】:

标签: android button


【解决方案1】:

所以我找到了一个解决方案,并将其添加到功能读者这里。诀窍是使用 . 我在这里找到了答案http://belencruz.com/2012/12/rounded-button-with-shadow-in-android/ 我现在的代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">


<item
    android:state_pressed="true"
    android:state_enabled="true"
    >
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/btn" /> 
<item android:drawable="@color/transparent"/>


</layer-list>
</item>
<item
    android:drawable="@drawable/btn" />
</selector>

真的很好用!

【讨论】:

【解决方案2】:

尝试将背景声明为阴影效果,将源声明为按钮图像:

   <ImageButton
        android:background="@android:drawable/dialog_holo_light_frame"
        android:src="@drawable/books"
   />

【讨论】:

    【解决方案3】:

    上述解决方案对我不起作用。没有任何东西可以代替按钮显示。以下是我的有效代码:

    首先:添加到layout file

    android:background="@drawable/new_button"
    

    第二步:创建new_button.xml文件,其中show3是图片文件

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:state_pressed="true">
        <layer-list>
          <item android:left="4dp" android:top="4dp">
            <bitmap android:src="@drawable/show3"/>                
          </item>
        </layer-list>
      </item>
      <item>
        <layer-list>
          <!-- SHADOW LAYER -->
          <item android:left="4dp" android:top="4dp">
            <shape>
              <solid android:color="#66000000" />
              <corners android:radius="10dip"/>
            </shape>
          </item>
          <!-- CONTENT LAYER -->
          <item android:bottom="4dp" android:right="4dp">
            <bitmap android:src="@drawable/show3"/>            
          </item>
        </layer-list>
      </item>
    </selector>
    

    基本上必须将位图添加到项目。

    【讨论】:

      猜你喜欢
      • 2018-12-18
      • 2017-04-14
      • 1970-01-01
      • 1970-01-01
      • 2018-05-20
      • 2014-08-20
      • 1970-01-01
      • 2011-09-27
      相关资源
      最近更新 更多