0
我正在開發一個小型應用程序作爲學校項目,並且我需要動態地移動FAB。 我在相對佈局內有一個FloatingActionButton,它實際上是底部對齊和水平對齊的。元素在RelativeLayout上設置邊距後消失
<RelativeLayout 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="com.pagiaro.nicola.quiz2_0.HomeActivity"
android:id="@+id/home_rl" >
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
app:fabSize="normal"
app:srcCompat="@drawable/ic_quiz"
android:id="@+id/fab_inizia"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginBottom="@dimen/activity_vertical_margin"
app:backgroundTint="@color/accentColor" />
</RelativeLayout>
我使用移動的代碼是:
final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_inizia);
final RelativeLayout.LayoutParams rlp_keyboard_is_open = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp_keyboard_is_open.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlp_keyboard_is_open.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
fab.setLayoutParams(rlp_keyboard_is_open);
但有了這個代碼,我FAB有不對的,底邊距。當我添加代碼來添加邊距時,問題就出現了。
rlp_keyboard_is_open.setMargins(0, 0, R.dimen.activity_horizontal_margin, R.dimen.activity_vertical_margin);
或
rlp_keyboard_is_open.rightMargin = R.dimen.activity_horizontal_margin;
rlp_keyboard_is_open.bottonMargin = R.dimen.activity_vertical_margin;
如果我添加該代碼,FAB消失。 我搜索了很多的解決方案,但我沒有找到它。任何幫助?謝謝。
它的工作原理!非常感謝! –