layout-代码中添加view

今天需要在代码中动态的给一个布局添加一个imageview,现在把方法记录如下。直接看demo代码

//创建容器
        final LinearLayout layout = new LinearLayout(this);
        //设置背景
        layout.setBackgroundColor(0x7700ffff);
        //设置垂直排列
        layout.setOrientation(LinearLayout.VERTICAL);
        //设置控件水平居中
        layout.setGravity(Gravity.CENTER_HORIZONTAL);
        //创建属性
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(300,300);
        addContentView(layout, layoutParams);

        
        //创建控件image
        ImageView imageView = new ImageView(this);
        imageView.setBackgroundColor(0xffff0000);
        LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(100,100);
        
        //设置子控件的margin数值
        layoutParams2.leftMargin=50;
        layoutParams2.topMargin=50;
        //添加到容器
        layout.addView(imageView, layoutParams2);
        
        //创建textiew
        TextView textView = new TextView(this);
        textView.setText("textview");
        LinearLayout.LayoutParams layoutParams3 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
     //设置相对父容器的位置
//layoutParams.gravity = Gravity.END;
//设置自身内容相对自身位置
//button.setGravity(Gravity.END); //设置padding layoutParams3.leftMargin=70; layoutParams3.topMargin=20; layout.addView(textView, layoutParams3);

 

看效果图

其实,代码中动态添加view,就跟使用xml一样的,xml中有的属性,都可以通过代码设置实现。这些属性,要么通过创建的控件(imageview)方法来设,要么就是通过

LayoutParams

其实,在这个类里面主要含有的是margin相关属性,下面是它源码的一部分

            this.leftMargin = source.leftMargin;
            this.topMargin = source.topMargin;
            this.rightMargin = source.rightMargin;
            this.bottomMargin = source.bottomMargin;
            this.startMargin = source.startMargin;
            this.endMargin = source.endMargin;

 还有另外一个常用的relativelayout,我们也写一个小的demo看下

xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:id="@+id/aaa"
    android:layout_height="match_parent"
    tools:context="com.example.testcode.MainActivity" >

    <TextView 
        android:id="@+id/tv"
        android:text="origin text"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#ff0000"
        />

</RelativeLayout>

代码如下

package com.example.testcode;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
    private TextView textView;
    private RelativeLayout aaaLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.tv);
        aaaLayout = (RelativeLayout) findViewById(R.id.aaa);
        Button button = new Button(this);
        RelativeLayout.LayoutParams layoutParams_button = new RelativeLayout.LayoutParams(
                100, 100);    
        RelativeLayout relativeLayout = new RelativeLayout(this);
        //LinearLayout这个方法有三个参数的,加了一个权重在里面,RelativeLayout就不存在了
        //LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, height, weight);
        RelativeLayout.LayoutParams layoutParams_text = new RelativeLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);    
        //设置控件相对R.id.tv位置
        //layoutParams_text.addRule(RelativeLayout.BELOW, R.id.tv);
        //设置控件相对父容器位置
        layoutParams_text.addRule(RelativeLayout.CENTER_HORIZONTAL);
        layoutParams_text.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        //layoutParams_text.addRule(RelativeLayout.CENTER_IN_PARENT);
        relativeLayout.addView(button, layoutParams_button);
        aaaLayout.addView(relativeLayout, layoutParams_text);
    }

    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

我们看下效果




 

posted @ 2015-08-13 16:27  小白屋  阅读(1417)  评论(1编辑  收藏  举报