LayoutInflater

来源:互联网 发布:mac 10.7.5 dmg 编辑:程序博客网 时间:2024/06/08 08:12

include.xml

<?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="vertical" >
 <TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="输入内容"/>
 <EditText
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:hint="请输入内容"/>

</LinearLayout>



    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
        LinearLayout linearLayout = new LinearLayout(this);//动态创建LinearLayout
        linearLayout.setOrientation(LinearLayout.VERTICAL);//设置布局为vertical
        LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
        linearLayout.setLayoutParams(params);
        
        //布局片段
        LayoutInflater inflater = getLayoutInflater();
        
        View view1 = inflater.inflate(R.layout.include, null);
        View view2 = inflater.inflate(R.layout.include, null);
        
        linearLayout.addView(view1);
        linearLayout.addView(view2);
        setContentView(linearLayout);
    }

0 0
原创粉丝点击