Android——控制UI界面

1、使用XML佈局文件控制UI界面java

res\layout\activity_main.xml代碼以下:android

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/FrameLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background">
<TextView 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
	android:text="@string/title"
	style="@style/text"
    >
</TextView>
<TextView
    android:id="@+id/startButton"
	android:layout_gravity="center_vertical|center_horizontal"
	android:text="@string/start"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	style="@style/text"
    >
    
</TextView>
</FrameLayout>

一個TextView組件用於顯示提示文字,一個在窗體正中間顯示開始遊戲按鈕canvas

其中res\values\styles.xml代碼以下app

<resources xmlns:android="http://schemas.android.com/apk/res/android">
	<style name="text">
	    <item name="android:textSize">24dp</item>
	    <item name="android:textColor">#111111</item>
	</style>
</resources>

用於指定應用的樣式,指定文字的大小和顏色。ide

最後在主活動中也就是MainActivity中,應用一下代碼指定應用的佈局文件。佈局

setContentView(R.layout.activity_main);

2、用代碼來控制UI界面ui

package com.basil_lee.ui;

import android.os.Bundle;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.activity_main);
		FrameLayout frameLayout=new FrameLayout(this);//建立幀佈局管理器
		frameLayout.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.background));//設置背景圖片
		setContentView(frameLayout);//設置在Activity中顯示frameLayout
		//建立一個TextView組件
		TextView text1=new TextView(this);
		text1.setText("在代碼中控制UI界面");
		text1.setTextSize(TypedValue.COMPLEX_UNIT_PX,24);//設置文字的大小,單位爲像素
		text1.setTextColor(Color.rgb(1, 1, 1));
		frameLayout.addView(text1);
		//建立另一個TextView組件
		TextView text2=new TextView(this);
		text2.setText("單擊進入遊戲。。。。。。");
		text2.setTextSize(TypedValue.COMPLEX_UNIT_PX,50);//設置文字的大小,單位爲像素
		text2.setTextColor(Color.rgb(1, 1, 1));
		LayoutParams params=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);//建立保存佈局參數的對象
		params.gravity=Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL;
		text2.setLayoutParams(params);
		//爲text2組件添加單擊事件,並將組件添加到佈局管理器中
		text2.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				new AlertDialog.Builder(MainActivity.this).setTitle("系統提示")
					.setMessage("遊戲有風險,進入需謹慎,真的要進入嗎?")
					.setPositiveButton("肯定", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface arg0, int arg1) {
							Toast.makeText(MainActivity.this, "進入遊戲", Toast.LENGTH_LONG).show();
						}
					}).setNegativeButton("退出",new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface arg0, int arg1) {
							Toast.makeText(MainActivity.this, "退出遊戲", Toast.LENGTH_LONG).show();
							finish();
						}
					}).show();
			}
		});
		
	   frameLayout.addView(text2);
	}

}

3、雖然徹底經過XML佈局文件控制UI界面,實現起來比較方便快捷,可是有失靈活性;而徹底經過Java代碼控制UI界面,雖然比較靈活,可是開發過程比較繁瑣。所以經常二者結合。this

首先是界面佈局代碼:spa

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@drawable/background"
       android:id="@+id/mylayout">

</LinearLayout>

而後Java代碼以下:code

    setContentView(R.layout.test);
        LinearLayout linearLayout=(LinearLayout)findViewById(R.id.mylayout);
        for(int i=0;i<imgPath.length;i++){
            img[i]=new ImageView(this);//建立一個ImageView組件
            img[i].setImageResource(imgPath[i]);
            img[i].setPadding(5, 5, 5, 5);//設置ImageView組件的內邊距
            LayoutParams params=new LayoutParams(153,148);//設置組件的寬度和高度
            img[i].setLayoutParams(params);//爲ImageView組件設置佈局參數
            linearLayout.addView(img[i]);//將ImageView組件添加到佈局管理器中
        }

以上代碼在窗體中橫向並列的顯示4張圖片

4、除了以上三種咱們還能夠開發自定義的View

開發自定義的View組件大體分爲如下三個步驟:

  • 建立一個繼承android.view.View類的View類,而且重寫構造方法
  • 根據須要重寫相應的方法。
  • 在項目中建立並實例化自定義的View類,並將其添加到佈局管理器中。

下面是相似飛機大戰中的一個例子:

class MyView extends View{
	
	public float bitMapX;
	public float bitMapY;
	public MyView(Context context) {
		super(context);
		bitMapX=750;
		bitMapY=750;
	}
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		Paint paint=new Paint();
		Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.air1);
		canvas.drawBitmap(bitmap, bitMapX,bitMapY,paint);
		if(bitmap.isRecycled()){
			bitmap.recycle();
		}
	}
}

  

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test2);
		FrameLayout frameLayout=(FrameLayout)findViewById(R.id.mylayout);
		final MyView air=new MyView(getApplicationContext());
		air.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				air.bitMapX=arg1.getX();
				air.bitMapY=arg1.getY();
				air.invalidate();
				return true;
			}
		});
		frameLayout.addView(air);

這樣就可使飛機那種圖片跟隨者手指去移動了。

以上就是三種UI的控制方法。

相關文章
相關標籤/搜索