[Android Getting Started to Project Combat -- 11.1] -- Realize the floating button

        The floating button is suspended on the interface. When the screen is swiped, the button will not move with the screen. It is a control provided in the Design Support library. This control can help us easily realize the floating button effect.

Implementation of FloatingActionButton

        Let's implement a published floating button.

       Since you need to use floatingActionButton, import it first. You can refer to this article:

"Getting Started with Android Basics" Floating Button - Nuggets

        Prepare an icon and place it in the drawable directory.

        Modify the activity_main.xml code as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.getbase.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            app:fab_size="normal"
            android:src="@drawable/publish" />
    </FrameLayout>

</LinearLayout>

MainActivity code:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "点击了按钮", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

The effect is as follows:

 

Supongo que te gusta

Origin blog.csdn.net/Tir_zhang/article/details/130662083
Recomendado
Clasificación