๊ด€๋ฆฌ ๋ฉ”๋‰ด

ruriruriya

[Android] ์•ˆ๋“œ๋กœ์ด๋“œ - Floating Action Button ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ๋ณธ๋ฌธ

๐Ÿค–Android/Java

[Android] ์•ˆ๋“œ๋กœ์ด๋“œ - Floating Action Button ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

๋ฃจ๋ฆฌ์•ผใ…‘ 2024. 1. 2. 15:20
๋ฐ˜์‘ํ˜•

Floating Action Button (FAB)๋Š” ์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ์˜ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค(UI) ์š”์†Œ ์ค‘ ํ•˜๋‚˜๋กœ,
ํ™”๋ฉด์—์„œ ๋– ์žˆ๋Š” ์›ํ˜•์˜ ๋ฒ„ํŠผ์„ ๋งํ•œ๋‹ค.
์ผ๋ฐ˜์ ์œผ๋กœ ์•ฑ์˜ ์ฃผ์š” ์ž‘์—…์ด๋‚˜ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋™์ž‘์„ ๊ฐ•์กฐํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค.

 

1. XML ๋ ˆ์ด์•„์›ƒ์—์„œ FAB ์ถ”๊ฐ€

 activity_main.xml  ํŒŒ์ผ์„ ์—ด๊ณ  FAB๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fabAdd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="11dp"
    android:layout_marginBottom="12dp"
    android:clickable="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:srcCompat="@drawable/baseline_add_circle_24" />

 

2. MainActivity์—์„œ FAB ์„ค์ •

์•กํ‹ฐ๋น„ํ‹ฐ ํด๋ž˜์Šค์—์„œ FAB๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด
ํ•ด๋‹น FAB๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋“ฑ์˜ ์ž‘์—…์„ ํ•œ๋‹ค.

public class MainActivity extends AppCompatActivity {

    FloatingActionButton fabAdd;

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

        fabAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // ํด๋ฆญ์‹œ ํ•˜๊ณ  ์‹ถ์€ ์•ก์…˜...
            }
        });
    }
}

 

๋ฐ˜์‘ํ˜•