LoginSignup
0
1

More than 5 years have passed since last update.

[WIP] わざわざ自作するまでもないAndroidのUIコンポーネント

Posted at

常識的な話ですが、標準で提供されているのに自作している記事がかなり多い気がして気になったのでまとめておきたいと思います。他にもあるよという方はコメントなどで教えてください。

Ripple エフェクト

android:background="?android:selectableItemBackground"

テーマにもよりますが、 AppCompat 系のテーマかつ Android 5.0 以上ではリップルのエフェクトが出ます。自分で Drawable などをつくって対応すると互換性のために Android 5.0 未満向けの Drawable をつくる必要がありますが、その必要もないので楽です。

android:background="?android:selectableItemBackgroundBorderless"

Android 5.0 以上向け。
View の枠をはみ出して丸く描かれるリップルを出したいときに使います。ImageButton に使うことが多いんじゃないかなと思いますが、その場合にたいていタップ領域が狭すぎてタップしづらいみたいなことが起きるので padding で調整しましょう。

RecyclerView の区切り線

// import android.support.v7.widget.DividerItemDecoration
RecyclerView rv = findViewById(R.id.rv);
rv.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL))

縦か横に線を引いて区切るくらいなら簡単にできます。また、RecyclerView の LayoutManager は XML でも設定できます。

Dialog に ListView

AlertDialog.Builder にあります。わざわざ setView() する必要はありません。

// import android.app.AlertDialog
// OR
// import android.support.v7.app.AlertDialog
new AlertDialog.Builder(this)
    .setItems(new String[]{"1", "2"}, (dialog, which) -> {})
    .show();
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1