1 package com.android.test.layout;
2 
3 import android.app.Activity;
4 import android.content.Context;
5 import android.os.Build;
6 import android.os.Bundle;
7 import android.view.Gravity;
8 import android.view.View;
9 import android.widget.Button;
10 import android.widget.GridLayout;
11 import android.widget.TextView;
12 
13 import static android.widget.GridLayout.LayoutParams;
14 import static android.widget.GridLayout.LAYOUT_MODE_OPTICAL_BOUNDS;
15 
16 public class LayoutInsetsTest extends Activity {
17     static int[] GRAVITIES = {Gravity.LEFT, Gravity.LEFT, Gravity.CENTER_HORIZONTAL, Gravity.RIGHT, Gravity.RIGHT};
18 
create(Context context)19     public static View create(Context context) {
20         final int N = GRAVITIES.length;
21 
22         GridLayout p = new GridLayout(context);
23         p.setUseDefaultMargins(true);
24         //p.setAlignmentMode(ALIGN_BOUNDS);
25         p.setLayoutMode(LAYOUT_MODE_OPTICAL_BOUNDS);
26 
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 
47         }
48 
49 
50         return p;
51     }
52 
onCreate(Bundle savedInstanceState)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 }
60