如何增加 layout_marginBottom 像素数?
How to increase layout_marginBottom by a number of pixels?
在游戏中,我在 FrameLayout 中显示自定义视图和 FAB:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<de.slova.GameView
android:id="@+id/gameView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:src="@drawable/play_white" />
</FrameLayout>
但是 FAB 显示得太低,遮住了底部的浅蓝色条:
我需要将 layout_marginBottom
增加浅蓝色条的高度(如上面屏幕截图中的红色箭头所示),以便 FAB 向上移动。
我知道栏的高度(以像素为单位)(我的自定义视图的内容按矩阵缩放)并且我想我知道该操作的正确位置(我的自定义中的 onSizeChanged
方法视图)- 但我的困难是掌握 FAB 的 layout_marginBottom
值。
请问如何访问和修改?我试过:
@Override
protected void onSizeChanged(int w, int h, int oldW, int oldH) {
super.onSizeChanged(w, h, oldW, oldH);
int h = mBar.getHeight();
ViewGroup.LayoutParams params = mFab.getLayoutParams();
// how to add h to layout_marginBottom here?
mFab.setLayoutParams(params);
}
顺便说一下,如果需要,我有两种方法可以在 dp
和 px
之间进行转换:
public static float px2sp(Context context, float px) {
float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;
return px / scaledDensity;
}
public static float sp2px(Context context, float sp) {
float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;
return sp * scaledDensity;
}
边距 属性 是 FrameLayout.LayoutParams
Class 的一部分,因此您必须将其转换为 FrameLayout.LayoutParams
FrameLayout.LayoutParams param = (FrameLayout.LayoutParams) fab.getLayoutParams();
param.bottomMargin = mBar.getHeight();
fab.setLayoutParams(param);
在游戏中,我在 FrameLayout 中显示自定义视图和 FAB:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<de.slova.GameView
android:id="@+id/gameView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:src="@drawable/play_white" />
</FrameLayout>
但是 FAB 显示得太低,遮住了底部的浅蓝色条:
我需要将 layout_marginBottom
增加浅蓝色条的高度(如上面屏幕截图中的红色箭头所示),以便 FAB 向上移动。
我知道栏的高度(以像素为单位)(我的自定义视图的内容按矩阵缩放)并且我想我知道该操作的正确位置(我的自定义中的 onSizeChanged
方法视图)- 但我的困难是掌握 FAB 的 layout_marginBottom
值。
请问如何访问和修改?我试过:
@Override
protected void onSizeChanged(int w, int h, int oldW, int oldH) {
super.onSizeChanged(w, h, oldW, oldH);
int h = mBar.getHeight();
ViewGroup.LayoutParams params = mFab.getLayoutParams();
// how to add h to layout_marginBottom here?
mFab.setLayoutParams(params);
}
顺便说一下,如果需要,我有两种方法可以在 dp
和 px
之间进行转换:
public static float px2sp(Context context, float px) {
float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;
return px / scaledDensity;
}
public static float sp2px(Context context, float sp) {
float scaledDensity = context.getResources().getDisplayMetrics().scaledDensity;
return sp * scaledDensity;
}
边距 属性 是 FrameLayout.LayoutParams
Class 的一部分,因此您必须将其转换为 FrameLayout.LayoutParams
FrameLayout.LayoutParams param = (FrameLayout.LayoutParams) fab.getLayoutParams();
param.bottomMargin = mBar.getHeight();
fab.setLayoutParams(param);