【问题标题】:How to set button have image and shadow?如何设置按钮有图像和阴影?
【发布时间】:2017-08-01 06:55:59
【问题描述】:

要制作阴影,请制作阴影 xml 文件并使用 android:background="@drawable/shadow"。 但是图片也是android:background="@drawable/image

如何在一个按钮中设置按钮阴影和图像?

对不起,我的英语不流利。

【问题讨论】:

  • 拍照按钮
  • 您可以在卡片视图中添加按钮以获得更好的阴影效果
  • 我找到了一个代码make shadow。但它必须使用android:backgroundoption。但是将图像放入按钮也使用android:background。我想知道如何同时使用它...
  • 你可以使用 Imagebutton

标签: android android-layout android-button


【解决方案1】:

1.使用图片按钮

试试这个,你可以使用ImageButton :- 显示一个带有用户可以按下或单击的图像(而不是文本)的按钮。默认情况下,ImageButton 看起来像一个普通的 Button,标准的按钮背景会在不同的按钮状态下改变颜色。

 <ImageButton
  android:id="@+id/btnMain"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:adjustViewBounds="true"
  android:contentDescription="@string/btnMain_description"
  android:elevation="10dp"
  android:background="@android:drawable/dialog_holo_light_fram‌​e"
  android:scaleType="centerInside"
  android:src="@drawable/IMage"
 />

2。用户 CardVirew 或者你可以试试这个在card view 中添加你的button 像这样

编译此依赖项

compile 'com.android.support:cardview-v7:25.3.1'

这样的布局

    <android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="10dp">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/disha"
        android:text="@string/app_name" />
</android.support.v7.widget.CardView>

如有任何疑问,请咨询我

【讨论】:

  • 在 ImageButton 处,图像没有完全填满。如何解决?
  • @DongHyeongJeong 检查更新的用户 android:layout_height="wrap_content"
  • 当然,我现在正在尝试:)
  • 在 CardView 中,如何添加阴影?
  • 使用 app:cardElevation="10dp" 在 cardview 中提供阴影
【解决方案2】:

在 res/drawable 中创建 button_selector.xml :

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <layer-list>
            <item android:right="5dp" android:top="5dp">
                <shape>
                    <corners android:radius="3dp" />
                    <solid android:color="#D6D6D6" />
                </shape>
            </item>
            <item android:bottom="2dp" android:left="2dp">
                <shape>
                    <gradient android:angle="270" 
                        android:endColor="#E2E2E2" android:startColor="#BABABA" />
                    <stroke android:width="1dp" android:color="#BABABA" />
                    <corners android:radius="4dp" />
                    <padding android:bottom="10dp" android:left="10dp" 
                        android:right="10dp" android:top="10dp" />
                </shape>
            </item>
        </layer-list>
    </item>

    </selector>

在你的 xml 布局中:

 <ImageButton
            android:src="@mipmap/ic_launcher"
            android:background="@drawable/shadow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:elevation="10dp"/>

【讨论】:

    【解决方案3】:

    使用CardView 来实现:

    将支持库添加到dependencies,如下:

    dependencies {
    compile 'com.android.support:cardview-v7:25.2.0'//Add respective version
      }
    

    在布局中使用它

     <android.support.v7.widget.CardView
                    xmlns:card_view="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/card_view"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/user_image"
                    app:cardBackgroundColor="@android:color/white"
                    android:foreground="?android:attr/selectableItemBackground"
                    android:layout_marginLeft="@dimen/activity_horizontal_margin"
                    android:layout_marginRight="@dimen/activity_horizontal_margin"
                    android:layout_marginTop="@dimen/padding_small"
                    android:layout_marginBottom="@dimen/padding_small"
                    app:cardCornerRadius="4dp"
                    app:cardElevation="10dp" > 
    

    如果有帮助,请立即告诉我。

    【讨论】:

      猜你喜欢
      • 2011-09-18
      • 2012-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多