试试这个:)
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ncrypted.floatingactionbuttoncenter.MainActivity">
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
public class MainActivity extends AppCompatActivity {
FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fab = (FloatingActionButton) findViewById(R.id.fab);
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
lp.gravity = Gravity.CENTER;
fab.setLayoutParams(lp);
}
}
在我的情况下有效:)