Button zu CustomView hinzufügen

  • Antworten:0
Mr. Storm
  • Forum-Beiträge: 4

28.12.2015, 03:47:20 via Website

Hallo,
ich habe eine Klasse DrawingView.java, in der der Nutzer ein Rechteck erstellen/zeichnen kann. An den Ecken dieser Rechtecke sollen RadioButtons erscheinen. Bei mir scheitert es leider daran, dass ich es nicht hinbekomme, generell einen Button zu dieser View hinzuzufügen. Kann mir da jemand bei helfen?

public class DrawingView extends View {

protected Paint mPaint;
protected Paint mPaintFinal;
protected Bitmap mBitmap;
protected Canvas mCanvas;

Button b;

public DrawingView(Context context) {
    super(context);
    init();
}

public DrawingView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
    b = new Button(context);
    b.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.WRAP_CONTENT,
            AbsListView.LayoutParams.WRAP_CONTENT));
    b.setText("Hello");
    FrameLayout f = (FrameLayout) findViewById(R.id.container);
    f.addView(b);
}

public DrawingView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

So funktioniert es leider nicht den Button zum "Container" hinzuzufügen.

Hier die weiteren Klassen:

MainActivity.java

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction().add(R.id.container, new ShapeFragment()).commit();
    }

}

}

MyFragment.java

public class MyFragment extends Fragment {

public MyFragment(){
    super();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_main, container, false);
}

}

activity_main.xml

<FrameLayout xmlns:android="..."
xmlns:tools="..."
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example..MainActivity"
tools:ignore="MergeRootFrame" >

fragment_main.xml

<RelativeLayout xmlns:android="..."
            xmlns:tools="..."
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin"
            tools:context="com.example..MainActivity$PlaceholderFragment">

<com.example..DrawingView
    android:id="@+id/drawingview"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

</com.example..DrawingView>

Antworten