【问题标题】:Android. Two exactly same buttons have different shadows安卓。两个完全相同的按钮有不同的阴影
【发布时间】:2017-06-28 18:30:42
【问题描述】:

在我的应用程序中,我有用户个人资料的屏幕。它有两个用于更改照片和密码的按钮。这是两个按钮的代码

....

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="19dp"
        android:gravity="center_vertical">

        <ImageView
            android:id="@+id/ivUserProfile"
            android:layout_width="59dp"
            android:layout_height="59dp"/>

        <Button
            style="@style/AppTheme.RaisedButton"
            android:id="@+id/btnSwitchPhoto"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:theme="@style/AppTheme.White"
            android:text="@string/change_avatar"
            android:textSize="16sp"
            android:textColor="@color/colorPrimary"
            android:layout_marginRight="17dp"
            android:layout_marginLeft="18dp"/>

</LinearLayout>

....

<Button
    style="@style/AppTheme.RaisedButton"
    android:id="@+id/btnSwitchPassword"
    android:layout_width="wrap_content"
    android:layout_height="48dp"
    android:layout_marginTop="28dp"
    android:theme="@style/AppTheme.White"
    android:text="@string/changePassword"
    android:textSize="16sp"
    android:textColor="@color/colorPrimary"/>

这些按钮完全相同(文本除外),其中一个位于LinearLayout 中,并且在 Android Studio 中预览时看起来相同。但是当我在设备上打开这个屏幕时,我看到按钮有不同的阴影。这是屏幕截图

有什么问题吗?

【问题讨论】:

    标签: android android-layout button


    【解决方案1】:

    这是正确的。

    Android 有两种类型的阴影 - 环境光和全向光。两者都会导致视图投射阴影。第一个是从环境反射的光,几乎无处不在,并在每个视图周围生成一个静态的、简单的灰色。

    第二个更动态,并导致阴影随着视图的位置而变化。全向光源位于屏幕上方。就像在现实世界中一样,如果某物离光越远,它的影子就会移动得越多。很难注意到,但底部按钮左侧的阴影多于右侧。那是因为它向左移动了。

    【讨论】:

      【解决方案2】:

      对两个按钮都使用 android:elevation="10dp" 看看是否有帮助

      【讨论】:

      • 没用
      【解决方案3】:
          Looks andoid:layout_width is set to match_parent in the first case and in the second it is wrap_content. If it is wrap_content, according to the text, the button size shrinks. 
      Moreover first one is inside linearlayout and the second one is outside and whether is it in any layout like Relativelayout?
      Attributes are different from each layout
      May be you keep both under same linearlayout and give a try by modifying only the text alone.
      
      Hope that helps!!!
      

      【讨论】:

      • 我尝试将两个底部都放在同一个父容器(LinearLayout)中,并将第二个按钮的宽度设置为 match_parent,但仍然存在同样的问题。我也尝试使用其他视图(具有自定义背景和高度的 CardView 和 TextView)而不是 Button,但问题仍然存在:第二个视图的阴影大于第一个视图的阴影
      • 按钮中的文本比第一个大。您尝试使用第一个按钮文本本身,如果没有任何阴影,这是主要原因。
      • 我尝试为两个按钮设置相同的文本。没用
      猜你喜欢
      • 2013-08-01
      • 2018-08-16
      • 2017-11-16
      • 2018-02-07
      • 2017-10-15
      • 2020-09-27
      • 2017-03-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多