Java 如何以编程方式更改应用程序:海拔
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37144961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change app:elevation programmatically
提问by Eraseth
My app is work for API 19. So I can't use android:elevation. So I app:elevation in my layout.
我的应用程序适用于 API 19。所以我不能使用 android:elevation。所以我 app:elevation 在我的布局中。
Android XML: android:elevation vs. app:elevation
Android XML:android:elevation 与 app:elevation
For exemple :
举个例子 :
<android.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:elevation="5dp">
How to change app:elevation programmatically ? I can change android:elevation but I can't find how to change app:elevation !
如何以编程方式更改应用程序:海拔?我可以更改 android:elevation 但我找不到如何更改 app:elevation !
采纳答案by Masum
Try with using the following code
尝试使用以下代码
ViewCompat.setElevation(View, float)
Following the below link. Here showed how to make elevation on pre-lollipop device
按照下面的链接。这里展示了如何在 pre-lollipop 设备上进行提升
回答by Chris Sherlock
Since you try to change the elevation of the Action bar you can easily use this line, which will work on API 19
由于您尝试更改操作栏的高度,您可以轻松使用此行,该行适用于 API 19
getSupportActionBar().setElevation(0);
or you follow this link and check out how to change the elevation of views on Android below API 21 generally
或者您点击此链接并查看如何在 API 21 以下的 Android 上更改视图的高度
回答by Vijay
Accepted answer didn't work for me.
接受的答案对我不起作用。
To remove shadow in Toolbar use
删除工具栏中的阴影使用
app:elevation="0"
in AppBarLayout xml or
app:elevation="0"
在 AppBarLayout xml 或
use mAppBarLayout.setTargetElevation(0)
in java.
mAppBarLayout.setTargetElevation(0)
在java中使用。
It worked in my case.
它在我的情况下有效。
回答by DrMabuse
The following solution is for NativeScript
以下解决方案适用于 NativeScript
let nativeAnView = tnsView.android;
var shape = new android.graphics.drawable.GradientDrawable();
shape.setShape(android.graphics.drawable.GradientDrawable.OVAL);
shape.setColor(android.graphics.Color.parseColor('#30bcff'));
nativeAnView.setBackgroundDrawable(shape);
nativeAnView.setElevation(20);
on Android
在安卓上
let nativeView = tnsView.ios;
nativeView.layer.shadowColor = new Color('#888888').ios.CGColor;
nativeView.layer.shadowOffset = CGSizeMake(0, 2.0);
nativeView.layer.shadowOpacity = 0.5;
nativeView.layer.shadowRadius = 5.0;
on iOS
在 iOS 上
回答by Archie G. Qui?ones
The accepted answer won't actually work. The answer should have been updated.
接受的答案实际上不起作用。答案应该已经更新了。
If you look at why mAppBarLayout.setTargetElevation(0)
is deprecated. it states that:
如果你看看为什么mAppBarLayout.setTargetElevation(0)
被弃用。它指出:
@deprecated target elevation is now deprecated. AppBarLayout's elevation is now controlled via a {@link android.animation.StateListAnimator}. This method now always returns 0.
@deprecated 目标高度现已弃用。AppBarLayout 的高度现在通过 {@link android.animation.StateListAnimator} 控制。此方法现在总是返回 0。
So to programmatically set the appbar layout you can only do it by creating a StateListAnimator
such as appbar_state_unelevated_animator
at the animator
res folder:
因此,要以编程方式设置 appbar 布局,您只能通过在res 文件夹中创建StateListAnimator
诸如:appbar_state_unelevated_animator
animator
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<objectAnimator
android:propertyName="elevation"
android:valueTo="4dp"
android:valueType="floatType"
android:duration="100"/>
</item>
then loading it in code like so:
然后在代码中加载它,如下所示:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
appbar.stateListAnimator = AnimatorInflater.loadStateListAnimator(applicationContext, R.animator.appbar_state_unelevated_animator)
}
If I'm not mistaken you could also create a StateListAnimator
by code but don't take my word for it.
如果我没记错的话,您也可以StateListAnimator
通过代码创建一个,但不要相信我的话。
Update:
更新:
In my experience when you set the app:elevation="//Any DP value//"
根据我设置 app 时的经验:elevation="//Any DP value//"
The Shadow of the app bar will be lost. Simple remove that line to get the appbar shadow again.
应用栏的阴影将丢失。简单地删除该行以再次获取应用栏阴影。
回答by Michalsx
I would like to provide updated and full answer:
我想提供更新和完整的答案:
switch (state) {
case On:
appBarLayout.setStateListAnimator(AnimatorInflater.loadStateListAnimator(getContext(), R.animator.appbar_elevation_on));
break;
case Off:
appBarLayout.setStateListAnimator(AnimatorInflater.loadStateListAnimator(getContext(), R.animator.appbar_elevation_off));
break;
}
appbar_elevation_on.xml under RES\animator
RES\animator 下的 appbar_elevation_on.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="PrivateResource">
<item>
<objectAnimator
android:propertyName="elevation"
android:valueTo="@dimen/design_appbar_elevation"
android:valueType="floatType" />
</item>
</selector>
appbar_elevation_off.xml under RES\animator
RES\animator 下的 appbar_elevation_off.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="PrivateResource">
<item>
<objectAnimator
android:propertyName="elevation"
android:valueTo="0dp"
android:valueType="floatType" />
</item>
</selector>