Android 自動GUI物件大小調整與配置設定(純手工方式)
Android 自動GUI物件大小調整與配置設定(純手工方式)
程式碼:
package com.example.absoluted_4button_full;
import com.example.absoluted_4button_full.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
///////////////////////////////////////
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.view.*;
import android.widget.*;
import android.view.ViewGroup.MarginLayoutParams;
///////////////////////////////////////
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/////////////////////////////////////////
requestWindowFeature(Window.FEATURE_NO_TITLE); //全螢幕設定
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
/////////////////////////////////////////
AbsoluteLayout AbsLayout1=new AbsoluteLayout(this);
setContentView(AbsLayout1);
/////////////////////////////////////////
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Button[] bt=new Button[4];
bt[0]=new Button(this);
bt[0].setText("0");
bt[0].setWidth(metrics.widthPixels/2);
bt[0].setHeight(metrics.heightPixels/2);
bt[0].setLayoutParams( new AbsoluteLayout.LayoutParams( metrics.widthPixels/2, metrics.heightPixels/2, 0, 0 ) );
//bt[0].layout(0, 0, metrics.widthPixels/2, metrics.heightPixels/2);
AbsLayout1.addView(bt[0]);
bt[1]=new Button(this);
bt[1].setText("1");
bt[1].setWidth(metrics.widthPixels/2);
bt[1].setHeight(metrics.heightPixels/2);
bt[1].setLayoutParams( new AbsoluteLayout.LayoutParams( metrics.widthPixels/2, metrics.heightPixels/2, metrics.widthPixels/2, 0 ) );
//bt[1].layout(metrics.widthPixels/2, 0, 0, metrics.heightPixels/2);
AbsLayout1.addView(bt[1]);
bt[2]=new Button(this);
bt[2].setText("2");
bt[2].setWidth(metrics.widthPixels/2);
bt[2].setHeight(metrics.heightPixels/2);
bt[2].setLayoutParams( new AbsoluteLayout.LayoutParams( metrics.widthPixels/2, metrics.heightPixels/2, 0, metrics.heightPixels/2 ) );
//bt[2].layout(0, metrics.heightPixels/2, metrics.widthPixels/2, 0);
AbsLayout1.addView(bt[2]);
bt[3]=new Button(this);
bt[3].setText("3");
bt[3].setWidth(metrics.widthPixels/2);
bt[3].setHeight(metrics.heightPixels/2);
bt[3].setLayoutParams( new AbsoluteLayout.LayoutParams( metrics.widthPixels/2, metrics.heightPixels/2, metrics.widthPixels/2, metrics.heightPixels/2 ) );
//bt[3].layout(l, t, r, b);
//bt[3].layout(metrics.widthPixels/2, metrics.heightPixels/2, 0, 0);
AbsLayout1.addView(bt[3]);
//TextView TextView1 = (TextView)findViewById(R.id.TextView01);
//TextView1.setText("手機銀幕大小為 "+metrics.widthPixels+" X "+metrics.heightPixels);
/////////////////////////////////////////
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
|