読者です 読者をやめる 読者になる 読者になる

textViewを中央寄せする

android
package com.example.xxx;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Canvas;
import android.view.Menu;
import android.widget.*;
import android.content.*;
import android.graphics.*;
import android.view.*;

public class MainActivity extends Activity {
	
	public TextView textView;
	public LinearLayout layout;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        LinearLayout linearLayout = new LinearLayout(this);
        setContentView(linearLayout);
        
        WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE);
        
        Display disp = wm.getDefaultDisplay();
        Point size = new Point();
        disp.getSize(size);
    	String width = "Width = " + size.x;
    	String height = "Height = " + size.y;
        
        linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
        
        textView = new TextView(this);
        textView.setText("試験");
        linearLayout.addView(textView);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    class MyView extends View{
    	public MyView(Context context){
    		super(context);
    	}
    	
    	@Override
    	protected void onDraw(Canvas canvas){
    		Paint paint = new Paint();
    		paint.setColor(Color.argb(255, 255, 0, 0));
    		canvas.drawRect(0, 0, 100, 100, paint);
    	}
    }
}