«

Activity添加新View

时间:2024-3-2 17:31     作者:韩俊     分类: Android



protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.write_annot);



SPSlate slate = new SPSlate(this); //view

slate.setPenSize(2, 10);

slate.setPenColor(Color.RED);

addContentView(slate, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));


}


第二种:通过RelativeLayout, 来控制后添加的View的大小:大小填充的是RelativeLayout的大小。

RelativeLayout mLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.write_annot);

SPSlate slate = new SPSlate(this);
slate.setPenSize(2, 10);
slate.setPenColor(Color.RED);

mLayout = (RelativeLayout)findViewById(R.id.writeAnnotateView);
mLayout.addView(slate, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

}


xml: 配置文件

<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal"
android:background="@android:color/darker_gray">

<ImageButton android:id="@+id/confirmBtn"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_weight="1"
android:background="@drawable/button"
android:layout_gravity="center_vertical"
android:src="@drawable/c_brushescheckmark_disabled"/>
<ImageButton android:id="@+id/reDoBtn"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_weight="1"
android:background="@drawable/button"
android:layout_gravity="center_vertical"
android:src="@drawable/c_redo_disabled"/>
<ImageButton android:id="@+id/unDoBtn"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_weight="1"
android:background="@drawable/button"
android:layout_gravity="center_vertical"
android:src="@drawable/c_undo_disabled"/>
<ImageButton android:id="@+id/fontBtn"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_weight="1"
android:background="@drawable/button"
android:layout_gravity="center_vertical"
android:src="@drawable/icon_font"/>
<ImageButton android:id="@+id/colorBtn"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_weight="1"
android:background="@drawable/button"
android:layout_gravity="center_vertical"
android:src="@drawable/icon_color"/>

</LinearLayout>

<RelativeLayout
android:id="@+id/writeAnnotateView"
android:layout_marginTop="48dp"
android:layout_width="match_parent"
android:layout_height="match_parent">

</RelativeLayout>


</RelativeLayout>



标签: android

热门推荐