Android 兩個絕對佈局元件/佈局元件隱藏與顯示/佈局上元件隱藏與顯示
Android 兩個絕對佈局元件/佈局元件隱藏與顯示/佈局上元件隱藏與顯示
自己撰寫一個程式兩個絕對佈局Layout,上面各放置兩個按鈕,並控制對應 按鈕/Layout 顯示與否,歡迎有興趣的同好一起來C/P一下。
package com.example.abs_abs_layout;
import com.example.abs_abs_layout.MainActivity.ButListener;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
///////////////////////////////////////
import android.view.View;
import android.util.DisplayMetrics;
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;
public class MainActivity extends Activity {
Button[] bt;
AbsoluteLayout AbsLayout2;
AbsoluteLayout AbsLayout1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/////////////////////////////////////////
requestWindowFeature(Window.FEATURE_NO_TITLE); //全螢幕設定
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
/////////////////////////////////////////
AbsLayout1=new AbsoluteLayout(this);
setContentView(AbsLayout1);
/////////////////////////////////////////
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
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].setFocusable(true);
bt[0].setOnClickListener(new ButListener());
bt[0].setLayoutParams( new AbsoluteLayout.LayoutParams( metrics.widthPixels/2, metrics.heightPixels/2, 0, 0 ) );
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].setOnClickListener(new ButListener());
bt[1].setLayoutParams( new AbsoluteLayout.LayoutParams( metrics.widthPixels/2, metrics.heightPixels/2, metrics.widthPixels/2, 0 ) );
AbsLayout1.addView(bt[1]);
AbsLayout2=new AbsoluteLayout(this);
AbsLayout2.setLayoutParams(new AbsoluteLayout.LayoutParams( metrics.widthPixels, metrics.heightPixels/2, 0, metrics.heightPixels/2));
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());
bt[2].setLayoutParams( new AbsoluteLayout.LayoutParams( metrics.widthPixels/2, metrics.heightPixels/2, 0, 0 ) );
AbsLayout2.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());
bt[3].setLayoutParams( new AbsoluteLayout.LayoutParams( metrics.widthPixels/2, metrics.heightPixels/2, metrics.widthPixels/2, 0 ) );
AbsLayout2.addView(bt[3]);
AbsLayout2.setVisibility(View.INVISIBLE);//hide
AbsLayout1.addView(AbsLayout2);
}
@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...");
bt[0].setVisibility(View.INVISIBLE);//hide
bt[1].setVisibility(View.VISIBLE);//show
AbsLayout2.setVisibility(View.INVISIBLE);//hide
}
if(v==bt[1])
{
bt[1].setText("1 clicked...");
bt[1].setVisibility(View.INVISIBLE);//hide
bt[0].setVisibility(View.VISIBLE);//show
AbsLayout2.setVisibility(View.VISIBLE);//hide
}
if(v==bt[2])
{
bt[2].setText("2 clicked...");
bt[2].setVisibility(View.INVISIBLE);//hide
bt[3].setVisibility(View.VISIBLE);//show
}
if(v==bt[3])
{
bt[3].setText("3 clicked...");
bt[3].setVisibility(View.INVISIBLE);//hide
bt[2].setVisibility(View.VISIBLE);//show
}
}
}
}
|