Material 设计:如何为 Android 浮动操作按钮设置透明度
Material Design : how to set transparency for Android Floating Action Button
我的问题与 一个有关。
当我打开NavigationDrawer 时,Floating Button 在上面但必须在下面。
我试着这样做:
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
Log.i(TAG , " inner onDrawerSlide");
super.onDrawerSlide(drawerView, slideOffset);
fabButton.setAlpha(25);
float alpha = 0.2f;
AlphaAnimation alphaUp = new AlphaAnimation(alpha, alpha);
alphaUp.setFillAfter(true);
fabButton.startAnimation(alphaUp);
syncState();
}
像那样:
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerClosed(view);
invalidateOptionsMenu();
fabButton.setAlpha(255);
syncState();
}
对我没有任何作用。有什么解决办法?
我的布局:
<mobapply.freightexchange.customviews.FragmentNavigationDrawer
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<!-- The ActionBar -->
<include
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- The main content view -->
<FrameLayout
android:id="@+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- The navigation drawer -->
<ListView
android:id="@+id/lvDrawer"
android:layout_width="match_parent"
android:divider="@null"
android:dividerHeight="0dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:background="#FFFFFF"
android:cacheColorHint="@android:color/transparent"
/>
FragmentNavigationDrawer 是自定义的 DrawerLayout
来自 Android documentation for setAlpha(float alpha)(自 API 11):
Sets the opacity of the view. This is a value from 0 to 1, where 0
means the view is completely transparent and 1 means the view is
completely opaque.
您正在尝试将其设置为值 25
或 255
:)
这应该用漂亮的动画隐藏它:
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
invalidateOptionsMenu();
fabButton.setAlpha(1 - slideOffset);
syncState();
}
或者,您可以将其滑出视图:
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
invalidateOptionsMenu();
fabButton.setTranslationX(slideOffset * 100);
syncState();
}
如果 fab 的位置有问题,那么让它可以拖动。
这是
的代码
private FloatingActionButton fab;
float dX;
float dY;
int lastAction;
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnTouchListener(this);
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
dX = view.getX() - event.getRawX();
dY = view.getY() - event.getRawY();
lastAction = MotionEvent.ACTION_DOWN;
break;
case MotionEvent.ACTION_MOVE:
view.setY(event.getRawY() + dY);
view.setX(event.getRawX() + dX);
lastAction = MotionEvent.ACTION_MOVE;
break;
case MotionEvent.ACTION_UP:
if (lastAction == MotionEvent.ACTION_DOWN) {
Intent intent = new Intent(Main.this, Cart.class);
startActivity(intent);
Toast.makeText(this, "Clicked!", Toast.LENGTH_SHORT).show();
}
break;
default:
return false;
}
return true;
}
我使用这行代码可以正常工作,但它可能与设备方向的变化有关。
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
fab.setAlpha(1-slideOffset);
}
我的问题与
当我打开NavigationDrawer 时,Floating Button 在上面但必须在下面。
我试着这样做:
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
Log.i(TAG , " inner onDrawerSlide");
super.onDrawerSlide(drawerView, slideOffset);
fabButton.setAlpha(25);
float alpha = 0.2f;
AlphaAnimation alphaUp = new AlphaAnimation(alpha, alpha);
alphaUp.setFillAfter(true);
fabButton.startAnimation(alphaUp);
syncState();
}
像那样:
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerClosed(view);
invalidateOptionsMenu();
fabButton.setAlpha(255);
syncState();
}
对我没有任何作用。有什么解决办法?
我的布局:
<mobapply.freightexchange.customviews.FragmentNavigationDrawer
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<!-- The ActionBar -->
<include
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- The main content view -->
<FrameLayout
android:id="@+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- The navigation drawer -->
<ListView
android:id="@+id/lvDrawer"
android:layout_width="match_parent"
android:divider="@null"
android:dividerHeight="0dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:background="#FFFFFF"
android:cacheColorHint="@android:color/transparent"
/>
FragmentNavigationDrawer 是自定义的 DrawerLayout
来自 Android documentation for setAlpha(float alpha)(自 API 11):
Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is completely transparent and 1 means the view is completely opaque.
您正在尝试将其设置为值 25
或 255
:)
这应该用漂亮的动画隐藏它:
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
invalidateOptionsMenu();
fabButton.setAlpha(1 - slideOffset);
syncState();
}
或者,您可以将其滑出视图:
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
invalidateOptionsMenu();
fabButton.setTranslationX(slideOffset * 100);
syncState();
}
如果 fab 的位置有问题,那么让它可以拖动。 这是
的代码private FloatingActionButton fab;
float dX;
float dY;
int lastAction;
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnTouchListener(this);
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
dX = view.getX() - event.getRawX();
dY = view.getY() - event.getRawY();
lastAction = MotionEvent.ACTION_DOWN;
break;
case MotionEvent.ACTION_MOVE:
view.setY(event.getRawY() + dY);
view.setX(event.getRawX() + dX);
lastAction = MotionEvent.ACTION_MOVE;
break;
case MotionEvent.ACTION_UP:
if (lastAction == MotionEvent.ACTION_DOWN) {
Intent intent = new Intent(Main.this, Cart.class);
startActivity(intent);
Toast.makeText(this, "Clicked!", Toast.LENGTH_SHORT).show();
}
break;
default:
return false;
}
return true;
}
我使用这行代码可以正常工作,但它可能与设备方向的变化有关。
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
fab.setAlpha(1-slideOffset);
}