Android 动态添加view addView

<LinearLayout
     android:id="@+id/lay_chart"
    android:layout_width="500dip"
    android:layout_height="500dip"
    android:orientation="vertical">
</LinearLayout>

父view

private LinearLayout mLaychart = null;
mLaychart = (LinearLayout) this.findViewById(R.id.lay_chart);      

子view

TextView t = new TextView(this);
t.setText("ziview");

父view添加子view

int width = DensityUtil.dip2px(getApplicationContext(), 300); //尺寸
int height = DensityUtil.dip2px(getApplicationContext(), 400);        
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(width, height);
      layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT); 
mLaychart.addView(t,layoutParams);

猜你喜欢

转载自blog.csdn.net/meixi_android/article/details/81028902