程序使用代碼控制UI屬性

<pre name="code" class="java">
package com.example.tryto;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class MainActivity extends Activity {
	private int top , left , right ,bottom; 
	private LinearLayout.LayoutParams tt = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
	private static boolean isChange = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a);
		final Button one = (Button)findViewById(R.id.one);
		
		one.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {			
				if(!isChange){	
					one.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
					isChange = true;
				}else{
					one.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
					isChange = false;
				}
			}
		});
		
		final Button up = (Button)findViewById(R.id.up);
		up.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Log.d("top", top+"");
				
				tt.setMargins(left, top, right, bottom);
				top--;bottom--;
				up.setLayoutParams(tt);
			}
		});
		
		final Button down =(Button)findViewById(R.id.down);
		down.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				tt.setMargins(left, top , right, bottom);
				top++;bottom++;
				down.setLayoutParams(tt);
			}
		});
		
		final Button r = (Button)findViewById(R.id.right);
		r.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				tt.setMargins(left, top , right, bottom);
				right++;left++;
				r.setLayoutParams(tt);
			}
		});
		
		final Button l = (Button)findViewById(R.id.left);
		l.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				tt.setMargins(left, top , right, bottom);
				left--;right--;
				l.setLayoutParams(tt);
			}
		});
	}
}

上面是利用代碼控制UI,獲得組件的引用,然後調用相關辦法就行了,對於不時變化的UI控件可以採用JAVA代碼進行控制,該程序只有6個按鈕,第一個按鈕能改變其自身的佈局方式,而上下左右四個按鈕能改變組件的填充空白值。最後一個只是爲了對比,沒有實際意義

其實我們也可以不針對UI組件單獨設置,可以針對佈局管理器來實現同樣的效果

以下是修改的源碼

package com.example.tryto;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class MainActivity extends Activity {
	private int top , left , right ,bottom; 
	private LinearLayout.LayoutParams tt = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
	private static boolean isChange = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a);
		final LinearLayout Linear = (LinearLayout)findViewById(R.id.root);
		final Button one = (Button)findViewById(R.id.one);
		
		one.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {			
				if(!isChange){	
					one.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
					isChange = true;
				}else{
					one.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
					isChange = false;
				}
			}
		});
		
		final Button up = (Button)findViewById(R.id.up);
		up.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				tt.setMargins(left, top, right, bottom);
				top--;bottom--;
				Linear.setLayoutParams(tt);
			}
		});
		
		final Button down =(Button)findViewById(R.id.down);
		down.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				tt.setMargins(left, top , right, bottom);
				top++;bottom++;
				Linear.setLayoutParams(tt);
			}
		});
		
		final Button r = (Button)findViewById(R.id.right);
		r.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				tt.setMargins(left, top , right, bottom);
				right++;left++;
				Linear.setLayoutParams(tt);
			}
		});
		
		final Button l = (Button)findViewById(R.id.left);
		l.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				tt.setMargins(left, top , right, bottom);
				left--;right--;
				Linear.setLayoutParams(tt);
			}
		});
	}
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章