2016-10-22 5 views
0

Ich entwickle eine kleine App als Projekt für die Schule, und ich muss einen FAB dynamisch verschieben. Ich habe ein FloatingActionButton in einem Relativen Layout, das tatsächlich unten ausgerichtet und horizontal ausgerichtet ist.Element verschwindet nach dem Einstellen der Ränder auf 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> 

Der Code, den ich für unterwegs bin mit es ist:

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); 

Aber mit diesem Code meine FAB nicht rechten und unteren Rand hat. Das Problem tritt auf, wenn ich Code hinzufüge, um Ränder hinzuzufügen.

rlp_keyboard_is_open.setMargins(0, 0, R.dimen.activity_horizontal_margin, R.dimen.activity_vertical_margin); 

oder

rlp_keyboard_is_open.rightMargin = R.dimen.activity_horizontal_margin; 
    rlp_keyboard_is_open.bottonMargin = R.dimen.activity_vertical_margin; 

Wenn ich diesen Code hinzufügen, verschwinden die FAB. Ich suchte nach einer Lösung, aber ich fand es nicht. Irgendeine Hilfe? Vielen Dank.

Antwort

0

Verwenden

getResources().getDimension(R.dimen.activity_horizontal_margin) 

oder

getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin) 

statt direkt mit R.dimen.activity_horizontal_margin, da sie die interne ID R.dimen.activity_horizontal_margin zugewiesen zurück, anstatt den Wert, den Sie in der XML-Datei zugeordnet.

+0

Es funktioniert! Vielen vielen Dank! –

Verwandte Themen