12
14

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.

layout_gravity や gravity を動的に設定する

Posted at

どっちがどっちかという話。

MyActivity.java
TextView tv = new TextView(this);
tv.setGravity(Gravity.CENTER);      // android:gravity="center" 相当
lp = new LinearLayout.LayoutParams( // 親 View の LayoutParams を指定
        ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.LEFT;          // android:layout_gravity="left" 相当
tv.setLayoutParams(lp);

…のはず。

参考

http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#attr_android:layout_gravity
http://developer.android.com/reference/android/widget/TextView.html#setGravity(int)

12
14
3

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
12
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?