【问题标题】:android:elevation doesn't work in L previewandroid:elevation 在 L 预览中不起作用
【发布时间】:2014-09-01 15:58:36
【问题描述】:

我已经玩了几天的 L 预览,但似乎不起作用的一件事是视图的 android:elevation 属性。调用View.setElevation() 时它工作得很好,但它似乎忽略了 XML 属性。我正在使用 Android Studio 0.8.2 并将minSdkVersiontargetSdkVersion 设置为'L',并将compileSdkVersion 设置为'android-L'buildToolsVersion"20.0.0"。这是一个无法正常工作的示例布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_margin="10dp"
        android:elevation="5dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="top"/>

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

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_margin="10dp"
        android:elevation="0dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="bottom"/>

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

</LinearLayout>

这是结果的截图:imgur.com/mqeq9vK

根据我设置的android:elevation,底牌应该平在“地面”上,但事实并非如此。事实上,它看起来与海拔是5dp 的顶牌没有什么不同。这是一个已知问题吗?它对您有用吗?

编辑:似乎这只是CardView 的情况,或者至少Buttons 不是问题。在这个布局中,我用Button 替换了CardView,即使阴影有点搞砸了(已知错误),你仍然可以看到高程的差异。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:elevation="2dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:elevation="1dp"/>

</LinearLayout>

编辑 2:几乎可以确认,此问题仅影响 CardViewandroid:elevation 适用于任何其他视图。有关为什么会发生这种情况的详细解释,请查看接受的答案。同时,我的解决方法是将CardView 替换为FrameLayout 并将android:background 设置为可绘制对象。这是一个有效的卡片背景可绘制示例:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#fff" />
</shape>

我也将此作为错误提交,所以如果它影响到您,请在此问题上加注星标。 https://code.google.com/p/android-developer-preview/issues/detail?id=731

【问题讨论】:

    标签: android android-5.0-lollipop android-elevation


    【解决方案1】:

    CardView 在初始化期间设置自己的高度,这将覆盖您从 XML 设置的任何内容。你应该把它作为一个错误提交到https://code.google.com/p/android-developer-preview/wiki/FilingIssues?tm=3

    @Override
    public void initialize(CardViewDelegate cardView, Context context, int backgroundColor,
            float radius) {
        cardView.setBackgroundDrawable(new RoundRectDrawable(backgroundColor, radius));
        View view = (View) cardView;
        view.setClipToOutline(true);
        view.setElevation(context.getResources().getDimension(R.dimen.cardview_elevation));
    }
    

    【讨论】:

    • 谢谢,这证实了我的怀疑。我会提交那个错误,同时我会想出一个不同的方法。
    • @alanv 你知道现在 Lollipop 正式出局这是否仍然是一个错误?我仍然无法让它在 XML 中工作。
    • 您应该使用 cardElevation 属性(正如 CardView 的开发者 yigit 的另一个回答中所述)。
    【解决方案2】:

    请使用cardElevation 属性在 CardView 上设置高度。这也会影响 pre-L 的阴影大小。

    另外,请参阅documentation 了解其他属性。

    【讨论】:

    • 这个好像是官方SDK发布后才添加的。尽管如此,还是感谢您让我知道这个属性!在 pre-lollipop 上轻松控制阴影大小的能力无疑是一个好处。
    【解决方案3】:

    请在你的根目录中添加这个

    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    

    下面是卡片视图的代码

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        card_view:cardElevation="10dp"
        android:id="@+id/view" >
        .
        .
        .
        .
        .
        Child
        .
        .
        .
        .
    
    </android.support.v7.widget.CardView>
    

    【讨论】:

      【解决方案4】:

      bug 修复发布之前,您可以通过编程方式对其进行设置。

      我已经在RecyclerView 中这样做了:

      在您的 RecyclerView.Adapter 中:

      public class MyAdapter extends RecyclerView.Adapter<TripsAdapter.ViewHolder> {
      
          /* ... */
      
          public class ViewHolder extends RecyclerView.ViewHolder {
              CardView card;
      
              public ViewHolder(View itemView) {
                  card = (CardView) itemView.findViewById(R.id.card_view);
              }
          }
      
          /* ... */
      
          @Override public void onBindViewHolder(ViewHolder holder, int position) {
              /// ...
      
              // Update Card Elevation
              final float scale = mContext.getResources().getDisplayMetrics().density;
              holder.card.setElevation(1f * scale);    //TODO set this in xml once CardView elevation bug is fixed
          }
      
      }
      

      【讨论】:

      • 不工作 setElevation 方法也只适用于android 5,所以还需要添加操作系统版本检查。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-28
      • 1970-01-01
      • 2016-11-15
      • 2018-09-01
      • 2015-03-06
      • 1970-01-01
      相关资源
      最近更新 更多