android布局之基础布局

目前我们使用的布局有五种:LinearLayout、FrameLayout、RelativeLayout、TableLayout、AbsoluteLayout

一、常见属性

第一类:属性值为true或false 
android:layout_centerHrizontal  水平居中 
android:layout_centerVertical   垂直居中 
android:layout_centerInparent    相对于父元素完全居中 
android:layout_alignParentBottom 贴紧父元素的下边缘 
android:layout_alignParentLeft   贴紧父元素的左边缘 
android:layout_alignParentRight  贴紧父元素的右边缘 
android:layout_alignParentTop    贴紧父元素的上边缘 
android:layout_alignWithParentIfMissing  如果对应的兄弟元素找不到的话就以父元素做参照物 


第二类:属性值必须为id的引用名“@id/id-name” 
android:layout_below      在某元素的下方 
android:layout_above      在某元素的的上方 
android:layout_toLeftOf   在某元素的左边 
android:layout_toRightOf  在某元素的右边 
android:layout_alignTop   本元素的上边缘和某元素的的上边缘对齐 
android:layout_alignLeft  本元素的左边缘和某元素的的左边缘对齐 
android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐 
android:layout_alignRight  本元素的右边缘和某元素的的右边缘对齐 

第三类:属性值为具体的像素值,如30dip,40px 
android:layout_marginBottom              离某元素底边缘的距离 
android:layout_marginLeft                离某元素左边缘的距离 
android:layout_marginRight               离某元素右边缘的距离 
android:layout_marginTop                 离某元素上边缘的距离 

android:background布局的背景
android:gravity布局中文字的位置,如:居中
android:layout_gravity整个布局的位置,如垂直居中等
android:weight,允许子元素可以填充屏幕上的剩余空间。这也避免了在一个大屏幕中,一串小对象挤成一堆的情况,而是允许他们放大填充空白。子元素指定一个 weight 值,剩余的空间就会按这些子元素指定的weight 比例分配给这些子元素。默认的 weight 值为0。例如,如果有三个文本框,其中两个指定了weight 值为1,那么,这两个文本框将等比例地放大,并填满剩余的空间,而第三个文本框不会放大。

二、TableLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >
	<TableLayout
	    android:layout_width="match_parent" android:layout_height="match_parent"
	    android:stretchColumns="*" android:shrinkColumns="*" >
	<TableRow android:background="#00ff00">
	    <EditText 
	        android:id="@+id/et_TextId" android:layout_width="fill_parent" 
	        android:text="" android:layout_span="3"
	        android:digits="+-*/0123456789"
	        />
	</TableRow>
	<TableRow android:background="#00ff00">
	    <Button android:id="@+id/bt_0" android:layout_width="1dip"
	        android:text="0"
	        />
	    <Button android:id="@+id/bt_1" android:layout_width="1dip"
	        android:text="1" 
	        />
	    <Button android:id="@+id/bt_2" android:layout_width="1dip"
	        android:text="2"
	        />	    
	</TableRow>
	<View 
	    android:layout_height="2dip" android:background="#334400"
	    />
	<TableRow android:background="#00ff00">
	    <Button android:id="@+id/bt_3" android:text="3" android:layout_width="1dip"/>
	    <Button android:id="@+id/bt_4" android:text="4" android:layout_width="1dip"/>
	    <Button android:id="@+id/bt_5" android:text="5" android:layout_width="1dip"/>	    
	</TableRow>
	</TableLayout>
</LinearLayout>

这边我们可以看到利用view呢可以实现分割线,注意android:stretchColumns="*" 可延伸列

android:shrinkColumns="*" 可收缩列  收缩从0开始的索引列。列直接必须用逗号隔开:1, 2, 5。非法或重复的设置将被忽略。
你可以通过"*"代替收缩所有列。注意一列能同时表示收缩和拉伸。这使得表格不会挤出屏幕

android:layout_span="3"合并三个单元格

andorid:layout_column="2"设置当前的位置在第二列 android:collapseColumns=0,2 意思是把第0和第2列隐藏


三、代码动态布局

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //LayoutInflater mInflate = LayoutInflater.from(this);
        //LinearLayout mLinearLayout = (LinearLayout)mInflate.inflate(R.layout.main, null).findViewById(R.id.layout_Id);
        LinearLayout mLinearLayout = new LinearLayout(this);
        LinearLayout.LayoutParams mLayoutParam = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
        mLinearLayout.setOrientation(LinearLayout.VERTICAL);
        mLinearLayout.setLayoutParams(mLayoutParam);
        
        Button bt = new Button(this);
        bt.setText("BUTTON");
        bt.setPadding(5, 5, 5, 5);
        bt.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        
        mLinearLayout.removeAllViews();
        mLinearLayout.addView(bt);
             
        setContentView(mLinearLayout);
    }
如果要把新增的view添加到xml定义的布局,就用代码中注释那两行。

	private static final int ET_ID = 1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RelativeLayout mRelativeLayout = new RelativeLayout(this);
        RelativeLayout.LayoutParams mLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mRelativeLayout.setLayoutParams(mLayoutParams);
        setContentView(mRelativeLayout);
        
        EditText et = new EditText(this);
        et.setBackgroundColor(Color.GREEN);
        et.setId(ET_ID);
        RelativeLayout.LayoutParams mEtLayout = new RelativeLayout.LayoutParams(100, ViewGroup.LayoutParams.WRAP_CONTENT);
        mEtLayout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        
        mRelativeLayout.removeAllViews();
        mRelativeLayout.addView(et,mEtLayout);
        
        Button bt = new Button(this);
        bt.setText("BUTTON");
        RelativeLayout.LayoutParams mBtLayout = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mBtLayout.addRule(RelativeLayout.BELOW, ET_ID);
        mBtLayout.addRule(RelativeLayout.ALIGN_LEFT, ET_ID);
        mRelativeLayout.addView(bt,mBtLayout);
    }
注意这里定义的id不能为0

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        TableLayout mTableLayout = new TableLayout(this);
        TableLayout.LayoutParams mLayoutParams = new TableLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mTableLayout.setLayoutParams(mLayoutParams);
        mTableLayout.setColumnStretchable(0, true);
        mTableLayout.setColumnStretchable(1, true);
        mTableLayout.setColumnStretchable(2, true);
        setContentView(mTableLayout);
        
        Button bt_1 = new Button(this);
        bt_1.setText("1");
        
        Button bt_2 = new Button(this);
        bt_2.setText("2");
        
        Button bt_3 = new Button(this);
        bt_3.setText("3");
        
        TableRow mTableRow = new TableRow(this);
        mTableRow.addView(bt_1);
        mTableRow.addView(bt_2);
        mTableRow.addView(bt_3);
        mTableLayout.setColumnStretchable(1, true);
        mTableLayout.addView(mTableRow);
        
    }











  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值