安卓 - 高度未生效

5

我正在将一个Android应用迁移到使用Material Design。为了验证我是否做得正确,我创建了一个空活动来测试一些Material Design项目(FAB、凸起按钮、视图高程、卡片、列表等等)。

按钮看起来很好,但是视图高程不太对。我在互联网上搜索了答案,但没有找到。我没有使用填充、透明背景等。以下是我的代码:

values-v21/styles.xml:

<resources>

    <style name="AppThemeNoActionBar" parent="AppTheme">
        <item name="android:navigationBarColor">@color/colorPrimary</item>
    </style>

    <style name="AppTheme" parent="MyMaterialTheme.Base">
        <item name="android:windowContentTransitions">true</item>
        <item name="android:windowAllowEnterTransitionOverlap">true</item>
        <item name="android:windowAllowReturnTransitionOverlap">true</item>
        <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
        <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
    </style>

    <style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="Button" parent="Base.Widget.AppCompat.Button.Colored">
        <item name="android:backgroundTint">@color/colorAccent</item>
    </style>

    <color name="colorPrimary">#E88768</color>
    <color name="colorPrimaryDark">#CC785C</color>
    <color name="colorAccent">#E88768</color>
    <color name="textColorPrimary">#000000</color>
    <color name="windowBackground">#FFFFFF</color>
    <color name="navigationBarColor">#E88768</color>
</resources>

layout-v21/start_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/white"
                xmlns:app="http://schemas.android.com/apk/res-auto">
    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentStart="true"
        android:elevation="12dp"
        android:translationZ="12dp"
        android:id="@+id/scrollView">

    </HorizontalScrollView>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_below="@+id/scrollView">

    </LinearLayout>

</RelativeLayout>

有人能帮我吗?

我并没有太涉及高度或Z轴的转换,但是高度是否会叠加到转换中呢?如果不是,那么你的translationZ和elevation不就会互相抵消吗?再次强调,我对此并不是很熟悉,这只是一个问题而已 :P - zgc7009
2个回答

8
尝试为您的HorizontalScrollView设置背景颜色/可绘制:
android:background="@android:color/white"

这似乎是绘制阴影所必需的。


2
我认为这实际上是一个特性 :) 原因是如果您不指定背景,它会隐式地变成透明的。由elevation属性创建的阴影会适应具有透明度的背景,这意味着阴影会尊重圆角等。因此,如果背景完全透明,则不会生成阴影。 - dthulke

0
如果那个elevation没起作用,你可以使用一个小技巧,把内容放在CardView里然后设置:
card_view:cardElevation="2dp"

Or:

card_view:cardCornerRadius="4dp"

For corners.

这是最好的方式,然而你可能在Android Studio的预览中看到了elevations,但实际上,当你编译应用程序或在模拟器或真实设备上运行时,所有这些都不起作用。因此,在这种情况下,最好使用CardView

例如:

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:foreground="?android:attr/selectableItemBackground"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="2dp">

        <HorizontalScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_alignParentStart="true"
            android:translationZ="12dp">

        </HorizontalScrollView>
    </android.support.v7.widget.CardView>

这个方法可行,但这只是一个变通方法,对吧?在 Android 的例子中,他们没有使用“CardView”来创建阴影。 - Elvis Oliveira
是的,例如,您可能希望在 preLollipop 上使用 Button 并带有类似 googleplay 设计的阴影,但您无法看到按钮的阴影或其他东西,那么我们需要像我的答案一样使用 CardView。只需将其视为另一种实现方式 :) - ʍѳђઽ૯ท

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接