Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 1 | package com.android.test.layout; |
| 2 | |
| 3 | import android.app.Activity; |
| 4 | import android.content.Context; |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5 | import android.os.Build; |
| 6 | import android.os.Bundle; |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 7 | import android.view.Gravity; |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 8 | import android.view.View; |
| 9 | import android.widget.Button; |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 10 | import android.widget.GridLayout; |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 11 | import android.widget.TextView; |
| 12 | |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 13 | import static android.widget.GridLayout.LayoutParams; |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 14 | import static android.widget.GridLayout.LAYOUT_MODE_OPTICAL_BOUNDS; |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 15 | |
| 16 | public class LayoutInsetsTest extends Activity { |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 17 | static int[] GRAVITIES = {Gravity.LEFT, Gravity.LEFT, Gravity.CENTER_HORIZONTAL, Gravity.RIGHT, Gravity.RIGHT}; |
| 18 | |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 19 | public static View create(Context context) { |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 20 | final int N = GRAVITIES.length; |
| 21 | |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 22 | GridLayout p = new GridLayout(context); |
| 23 | p.setUseDefaultMargins(true); |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 24 | //p.setAlignmentMode(ALIGN_BOUNDS); |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 25 | p.setLayoutMode(LAYOUT_MODE_OPTICAL_BOUNDS); |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 26 | |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 27 | p.setColumnCount(N); |
| 28 | |
| 29 | for (int i = 0; i < 2*N; i++) { |
| 30 | View c; |
| 31 | if (i % 2 == 0) { |
| 32 | TextView tv = new TextView(context); |
| 33 | tv.setTextSize(32); |
| 34 | tv.setText("A"); |
| 35 | c = tv; |
| 36 | } else { |
| 37 | Button b = new Button(context); |
| 38 | b.setBackgroundResource(R.drawable.btn_default_normal); |
| 39 | b.setText("B"); |
| 40 | c = b; |
| 41 | } |
| 42 | |
| 43 | LayoutParams lp = new LayoutParams(); |
| 44 | lp.setGravity(GRAVITIES[(i % N)]); |
| 45 | p.addView(c, lp); |
| 46 | |
Philip Milne | bbd51f1 | 2012-04-18 15:09:05 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 49 | |
| 50 | return p; |
| 51 | } |
| 52 | |
| 53 | protected void onCreate(Bundle savedInstanceState) { |
| 54 | super.onCreate(savedInstanceState); |
| 55 | //getApplicationInfo().targetSdkVersion = Build.VERSION_CODES.ICE_CREAM_SANDWICH; |
| 56 | getApplicationInfo().targetSdkVersion = Build.VERSION_CODES.JELLY_BEAN; |
| 57 | setContentView(create(this)); |
| 58 | } |
| 59 | } |