Android使用程序代码设置控件的margin属性

    android页面通常是通过布局的XML文件进行控制的,今天讲一下我自己个人在学习android的过程中通过代码生成页面,并且通过代码控制控件的大小和样式,先看一下最终的效果图:

    注:Linearlayout的背景是蓝色,Tablelayout的背景颜色为黄色,TextView的背景颜色为绿色。


1.1.DongAddActivity.java的文件代码为:

package com.simon.mytest;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.DefaultedHttpParams;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
/***
 *
 * @author simon
 *
 */
public class DongAddActivity extends Activity {
    TableLayout.LayoutParams FILL=new TableLayout.LayoutParams(android.widget.TableLayout.LayoutParams.WRAP_CONTENT, android.widget.TableLayout.LayoutParams.WRAP_CONTENT);
    TableLayout.LayoutParams Table_row_wrap=new TableLayout.LayoutParams(android.widget.TableLayout.LayoutParams.WRAP_CONTENT, android.widget.TableLayout.LayoutParams.WRAP_CONTENT);
    LinearLayout.LayoutParams WRAP_Tab=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    LinearLayout.LayoutParams l_FILL=new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//设置自定义标题栏
        final LinearLayout linear=new LinearLayout(this);
        TableLayout tablelayout=new TableLayout(this);
        //设置LiearLayout布局下面控件排序是横的排
        linear.setOrientation(LinearLayout.VERTICAL);
        TextView textview=null;
        TableRow row=null;
        TableRow.LayoutParams lp1=new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
        //设置Tablelayout布局的
        tablelayout.setLayoutParams(WRAP_Tab);
        //把Tablelayout添加到Linearlayout
        linear.addView(tablelayout);
        for (int i = 0; i < 100; i++) {
            textview=new TextView(this);
            textview.setText("text"+(i+1));
            //设置背景颜色
            textview.setBackgroundColor(Color.GREEN);
            textview.setWidth(70);
            textview.setHeight(50);
            lp1.setMargins(10, 10, 0, 0);
            /***
             * 在设置TextView控件的Margin的时候需要根据TextView所在的父控件的类型是什么
             * 比如,TextView的父控件是TableRow,则在设置TextView的Margin的时候,需要使用的LayoutParams
             * 是使用TableRow.LayoutParams的,不能使用
             */
            textview.setLayoutParams(lp1);
            textview.setOnClickListener(new MyOnclikeListener("text"+(i+1)));
            if(i%4==0){
                row=new TableRow(this);
                tablelayout.addView(row,Table_row_wrap);
            }
            row.addView(textview);
            
        }
        linear.setBackgroundColor(Color.BLUE);
        tablelayout.setBackgroundColor(Color.YELLOW);
        setContentView(linear,l_FILL);
        
    }
    class MyOnclikeListener implements OnClickListener{
        private String who;
        public MyOnclikeListener(String who){
            this.who=who;
        }

        @Override
        public void onClick(View arg0) {
            Toast.makeText(DongAddActivity.this, "Simon点击了:"+who, Toast.LENGTH_LONG).show();
            
        }
        
    }
    
}

1.2.以上是通过代码简单的设置控件布局和样式的,谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值