我正在尝试将a的高程设置AppBarLayout为0(并返回到默认值).它崩溃了一些时间(并不总是),只在API 21(> = 22等工作正常)崩溃.
我正在使用StateListAnimator以下链接中描述的(因为它是自support-lib 24.0.0以来设置高程的"官方"方式):
我的实际方法如下所示:
public void setAppBarElevation(final boolean visible) {
if (appBar != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
appBar.setStateListAnimator(AnimatorInflater.loadStateListAnimator(this, (visible) ? R.animator.appbar_elevated : R.animator.appbar_not_elevated));
}
}
Run Code Online (Sandbox Code Playgroud)
连同两个XML文件(另一个是设置android:valueTo="0"):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<objectAnimator android:propertyName="elevation"
android:valueTo="@dimen/default_appbar_elevation"
android:valueType="floatType"/>
</item>
</selector>
Run Code Online (Sandbox Code Playgroud)
我已经测试了一些具有不同Android版本的设备(API 19,21,23,24,25),并且它只在21上崩溃.
日志显示设置高程时出现了一些JNI错误(来自StateListAnimator我认为):
--------- beginning of system
JNI DETECTED ERROR IN APPLICATION: cant call void android.view.View.setElevation(float) on null object
in call to CallVoidMethodV
from …Run Code Online (Sandbox Code Playgroud) java-native-interface android android-support-library android-elevation android-appbarlayout
我有一个视图,我想使用 SVG/Vector 作为它的背景。所以它不是一个完美的方形、圆形或任何基本形状。我想要一个阴影来包围背景,但它不起作用。
我将设置高程,设置轮廓提供程序,甚至尝试将剪辑设置为填充,即使它正好在其布局的范围内。
<View
android:layout_width="200dp"
android:layout_height="300dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:outlineProvider="background"
android:background="@drawable/ic_bookmark_black_24dp"
android:elevation="5dp"/>
Run Code Online (Sandbox Code Playgroud)
(您可以使用可以从 Android Studio 导入的 Android 书签矢量资产自行测试)
仍然,没有阴影。我将背景更改为颜色的那一刻,它工作得很好
我已经玩了L预览了几天,有一件似乎不起作用的是android:elevation视图的属性.它在调用时工作得很好View.setElevation(),但它似乎忽略了XML属性.我正在使用Android Studio 0.8.2 minSdkVersion并targetSdkVersion设置'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>
Run Code Online (Sandbox Code Playgroud)
以下是结果的屏幕截图:imgur.com/mqeq9vK
根据android:elevation我的说法,底卡应该在"地面"上平放,但事实并非如此.事实上,它看起来与顶级卡的高度没有什么不同5dp.这是一个已知问题,它对你有用吗?
编辑:似乎只有这种情况CardView,或者至少它不是问题Buttons.在这个布局中我替换CardView了Button,即使阴影有点搞砸(已知的bug),你仍然可以看到高程的差异.
<?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 …Run Code Online (Sandbox Code Playgroud) 我试图重现Google在这里提出的高程示例:
https://developer.android.com/training/material/shadows-clipping.html
但对我来说,没有阴影出现.不在Android Studio设计器中,不在AVD模拟器上.我正在使用所有最新版本的Android Studio,SDK和模拟器图像.
我错过了什么?
如何在使用阴影的视图中显示高程ConstraintLayout?
使用Relative和Linear可以执行带阴影的高程来实现列表,但我不能这样做ConstraintLayout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="vertical">
<TextView
android:id="@+id/list_ssid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:elevation="8dp"
android:background="#fff"
android:text="SSID"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/guideline"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/list_ch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:elevation="8dp"
android:background="#fff"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="CH"
app:layout_constraintLeft_toLeftOf="@+id/guideline"
app:layout_constraintRight_toLeftOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/list_dB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:elevation="8dp"
android:background="#fff"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="dB"
app:layout_constraintLeft_toLeftOf="@+id/guideline2"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.65"
tools:layout_editor_absoluteX="239dp"
tools:layout_editor_absoluteY="0dp" />
<android.support.constraint.Guideline
android:id="@+id/guideline2" …Run Code Online (Sandbox Code Playgroud) 使用带有某些alpha(例如#99fe0038)的视图的背景颜色和API 21上的某些高程显示两个圆圈:一个用于视图本身,另一个用于内部:

通过代码设置高程和背景颜色:
view.setElevation(getResources().getDimensionPixelSize(R.dimen.fab_elevation_lollipop));
view.setBackgroundColor(Color.parseColor("#99fe0038"));
Run Code Online (Sandbox Code Playgroud)
没有设置高程或使用不透明的颜色,一切看起来都像预期的那样.
这是一个Android错误还是我错过了什么?
任何想法为什么RecyclerView项目变形,每个连续项目变得更糟?
更新:我正在使用CardViews,现在没有视图"变形",但阴影/海拔高度会发生变化.
看起来根据项目垂直位于屏幕上的位置,高程会变高,当项目向下滚动时阴影会增大.请在此处观看视频:https://youtu.be/nROYq8rpUMs.
我已经建立了一个新项目,只留下展示问题所需的代码:
MainActivity.java
package tzig.schm00.masterdetail;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import tzig.schm00.masterdetail.dummy.DummyContent;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View recyclerView = findViewById(R.id.item_list);
setupRecyclerView((RecyclerView) recyclerView);
}
private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
recyclerView.setAdapter(new SimpleItemRecyclerViewAdapter(DummyContent.ITEMS));
}
public class SimpleItemRecyclerViewAdapter
extends RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.ViewHolder> {
private final List<DummyContent.DummyItem> mValues;
public SimpleItemRecyclerViewAdapter(List<DummyContent.DummyItem> items) {
mValues = items;
}
@Override …Run Code Online (Sandbox Code Playgroud) android android-layout android-cardview android-elevation android-recyclerview
我正在使用'com.android.support:cardview-v7:23.4.0',我看到了阴影,但不是深灰色阴影,我想获得更深的高程阴影,但是我看不到有任何属性可以得到,有什么想法吗?
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
android:layout_marginBottom="8dp"
app:cardElevation="8dp"
app:cardPreventCornerOverlap="false"
app:cardCornerRadius="1dp"
app:cardUseCompatPadding="false">
<include layout="@layout/any_layout"/>
</android.support.v7.widget.CardView>
Run Code Online (Sandbox Code Playgroud) 我使用 Android Studio 创建了一个“基本活动”,并尝试使用toolbar-not-showing-elevation-in-android-9-api-28 中的答案在 API 28 Pixel 设备上创建高程阴影。但是,没有显示高程阴影。该activity_main.xml文件当前为:
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:elevation="0dp"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:elevation="16dp"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
为什么高程阴影不再起作用Toolbar?