2013年12月28日 星期六

透過FrameLayout 取代 AbsoluteLayout 範例

透過FrameLayout 取代 AbsoluteLayout 範例



 


由於本身程度很差,所以很喜歡AbsoluteLayout方式,但官網上又指出該Layout已經被...,所以開始爬文尋找替代方案,赫然發現有高手指出可以用FrameLayout 取代 AbsoluteLayout ,在喜出望外之於,趕緊寫一測試範例來實驗,確定可以之後,趕緊來PO出來,歡迎有興趣的同好,一起來C/P一下。


 


 









package com.example.framelayout_4button_full;

import com.example.framelayout_4button_full.MainActivity.ButListener;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
///////////////////////////////////////

import android.view.View;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.TextView;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.view.ViewGroup.MarginLayoutParams;
import android.app.ActionBar.LayoutParams;
import android.content.Context;

public class MainActivity extends Activity {
Button[] bt;
DisplayMetrics metrics;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/////////////////////////////////////////
requestWindowFeature(Window.FEATURE_NO_TITLE); //全螢幕設定
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
metrics = new DisplayMetrics();//抓取螢幕大小
getWindowManager().getDefaultDisplay().getMetrics(metrics);
/////////////////////////////////////////
FrameLayout Fralayout = new FrameLayout(this);
FrameLayout.LayoutParams lparam = new FrameLayout.LayoutParams(metrics.widthPixels,metrics.heightPixels);
Fralayout.setLayoutParams(lparam);
this.setContentView(Fralayout);
////////////////////////////////////////
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].setOnClickListener(new ButListener());
FrameLayout.LayoutParams layoutParams0 = new FrameLayout.LayoutParams(metrics.widthPixels/2, metrics.heightPixels/2);
layoutParams0.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams0.leftMargin = 0;
layoutParams0.topMargin = 0;
bt[0].setLayoutParams(layoutParams0);
Fralayout.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].setOnClickListener(new ButListener());
FrameLayout.LayoutParams layoutParams1 = new FrameLayout.LayoutParams(metrics.widthPixels/2, metrics.heightPixels/2);
layoutParams1.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams1.leftMargin = metrics.widthPixels/2;
layoutParams1.topMargin = 0;
bt[1].setLayoutParams(layoutParams1);
Fralayout.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].setOnClickListener(new ButListener());
FrameLayout.LayoutParams layoutParams2 = new FrameLayout.LayoutParams(metrics.widthPixels/2, metrics.heightPixels/2);
layoutParams2.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams2.leftMargin = 0;
layoutParams2.topMargin = metrics.heightPixels/2;
bt[2].setLayoutParams(layoutParams2);
Fralayout.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].setOnClickListener(new ButListener());
FrameLayout.LayoutParams layoutParams3 = new FrameLayout.LayoutParams(metrics.widthPixels/2, metrics.heightPixels/2);
layoutParams3.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams3.leftMargin = metrics.widthPixels/2;
layoutParams3.topMargin = metrics.heightPixels/2;
bt[3].setLayoutParams(layoutParams3);
Fralayout.addView(bt[3]);
}

@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;
}
class ButListener implements OnClickListener
{
public void onClick(View v)
{
if(v==bt[0])
{
bt[0].setText("0 clicked...");
}
if(v==bt[1])
{
bt[1].setText("1 clicked...");
}
if(v==bt[2])
{
bt[2].setText("2 clicked...");
}
if(v==bt[3])
{
bt[3].setText("3 clicked...");
}
}

}
}


 


沒有留言:

張貼留言