16
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ソースコード上でtextview のpaddingをdpで設定する

Posted at

textview のpaddingは通常レイアウトのxmlで指定しますが、adapterなどを利用している場合はソースコード上で設定したい時もあります。
その場合はhogeTv.setPadding(left,top, right, bottom)を利用できるわけですが、px指定となってしまうため、以下のようにしてpdに変換します。

int paddingDp = 10;  // dpを指定
float scale = getResources().getDisplayMetrics().density; //画面のdensityを指定。
int paddingPx = (int) (paddingDp * scale + 0.5f);
//(int) (paddingDp * scale)とした場合は端数は切り捨て
16
17
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
16
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?