| TextViewの文字位置配置 
 TextViewに文字を表示する場合、縦位置と横位置を決めたいことがあります。
 このサンプルとなります。
 
 
 
   
 
 
| import android.graphics.Color; import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 
 //import android.os.Build;
 //ログファイル出力
 //import android.util.Log;
 import android.view.Gravity;
 import android.widget.RelativeLayout;
 import android.widget.TextView;
 
 public class MainActivity extends AppCompatActivity {
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 //相対座標を設定できるようにレイアウトを設定
 RelativeLayout layout = new RelativeLayout(this);
 layout.setBackgroundColor(Color.WHITE);
 setContentView(layout);
 
 //座標の初期設定
 int x = 0;
 int y = 0;
 int width = 0;
 int height = 0;
 x = 50;
 y = 5;
 width = 380;//350;
 height = 70;//60;
 RelativeLayout.LayoutParams obj = getLayoutObject(width, height);
 obj.leftMargin=x;
 obj.topMargin=y;
 /*
 int iVersion=0;
 clsAPILevel cls =new clsAPILevel();
 iVersion=cls.getAPILevel();
 cls=null;
 */
 TextView txt1 = new TextView(this);
 txt1.setText("左上");
 txt1.setTextSize(20.0f);
 txt1.setBackgroundColor(Color.argb(70, 208, 208, 255));
 txt1.setTextColor(Color.BLACK);
 txt1.setTextSize(12.0f);
 txt1.setGravity(Gravity.LEFT|Gravity.TOP);
 layout.addView(txt1, obj);
 
 y+=height+10;
 obj = getLayoutObject(width, height);
 obj.leftMargin=x;
 obj.topMargin=y;
 TextView txt2 = new TextView(this);
 txt2.setText("中央上");
 txt2.setTextSize(20.0f);
 txt2.setBackgroundColor(Color.argb(70, 208, 208, 255));
 txt2.setTextColor(Color.BLACK);
 txt2.setTextSize(12.0f);
 txt2.setGravity(Gravity.CENTER|Gravity.TOP);
 layout.addView(txt2, obj);
 
 y+=height+10;
 obj = getLayoutObject(width, height);
 obj.leftMargin=x;
 obj.topMargin=y;
 TextView txt3 = new TextView(this);
 txt3.setText("右上");
 txt3.setTextSize(20.0f);
 txt3.setBackgroundColor(Color.argb(70, 208, 208, 255));
 txt3.setTextColor(Color.BLACK);
 txt3.setTextSize(12.0f);
 txt3.setGravity(Gravity.RIGHT|Gravity.TOP);
 layout.addView(txt3, obj);
 
 y+=height+10;
 obj = getLayoutObject(width, height);
 obj.leftMargin=x;
 obj.topMargin=y;
 TextView txt4 = new TextView(this);
 txt4.setText("左縦位置中央");
 txt4.setTextSize(20.0f);
 txt4.setBackgroundColor(Color.argb(70, 208, 208, 255));
 txt4.setTextColor(Color.BLACK);
 txt4.setTextSize(12.0f);
 txt4.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
 layout.addView(txt4, obj);
 
 y+=height+10;
 obj = getLayoutObject(width, height);
 obj.leftMargin=x;
 obj.topMargin=y;
 TextView txt5 = new TextView(this);
 txt5.setText("中央縦位置中央");
 txt5.setTextSize(20.0f);
 txt5.setBackgroundColor(Color.argb(70, 208, 208, 255));
 txt5.setTextColor(Color.BLACK);
 txt5.setTextSize(12.0f);
 txt5.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
 layout.addView(txt5, obj);
 
 y+=height+10;
 obj = getLayoutObject(width, height);
 obj.leftMargin=x;
 obj.topMargin=y;
 TextView txt6 = new TextView(this);
 txt6.setText("右縦位置中央");
 txt6.setTextSize(20.0f);
 txt6.setBackgroundColor(Color.argb(70, 208, 208, 255));
 txt6.setTextColor(Color.BLACK);
 txt6.setTextSize(12.0f);
 txt6.setGravity(Gravity.RIGHT|Gravity.CENTER_VERTICAL);
 layout.addView(txt6, obj);
 
 y+=height+10;
 obj = getLayoutObject(width, height);
 obj.leftMargin=x;
 obj.topMargin=y;
 TextView txt7 = new TextView(this);
 txt7.setText("左下");
 txt7.setTextSize(20.0f);
 txt7.setBackgroundColor(Color.argb(70, 208, 208, 255));
 txt7.setTextColor(Color.BLACK);
 txt7.setTextSize(12.0f);
 txt7.setGravity(Gravity.LEFT|Gravity.BOTTOM);
 layout.addView(txt7, obj);
 
 y+=height+10;
 obj = getLayoutObject(width, height);
 obj.leftMargin=x;
 obj.topMargin=y;
 TextView txt8 = new TextView(this);
 txt8.setText("中央下");
 txt8.setTextSize(20.0f);
 txt8.setBackgroundColor(Color.argb(70, 208, 208, 255));
 txt8.setTextColor(Color.BLACK);
 txt8.setTextSize(12.0f);
 txt8.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM);
 layout.addView(txt8, obj);
 
 y+=height+10;
 obj = getLayoutObject(width, height);
 obj.leftMargin=x;
 obj.topMargin=y;
 TextView txt9 = new TextView(this);
 txt9.setText("右下");
 txt9.setTextSize(20.0f);
 txt9.setBackgroundColor(Color.argb(70, 208, 208, 255));
 txt9.setTextColor(Color.BLACK);
 txt9.setTextSize(12.0f);
 txt9.setGravity(Gravity.RIGHT|Gravity.BOTTOM);
 layout.addView(txt9, obj);
 }
 //レイアウトを決定するオブジェクトを生成し渡します
 private RelativeLayout.LayoutParams getLayoutObject(int width,int height)
 {
 return new RelativeLayout.LayoutParams(width, height);
 }
 
 }
 
 
 |  
 実行結果
 
 
   
 
 
 |  |