简体   繁体   中英

How to add button to a view dynamically in android xamarin?

I am trying to add a button dynamically to a View in a droid app using xamarin.android. I need to be able to do this as i will have to add few things dynamically during the project. Heres the code i am trying out.

public class FeedbackFragment : Android.Support.V4.App.Fragment
{
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var view = inflater.Inflate (Resource.Layout.ChallengeFeedbackView, container, false);

        LinearLayout parentLayoutView = view.FindViewById (Resource.Id.feedbackView);//LinearLayout
        ScrollView feedbackscrollView = parentLayoutView.FindViewById (Resource.Id.scrollView1);//ScrolView

        var scrollviewInnerContainerView = feedbackscrollView.FindViewById (Resource.Id.scrollViewInnerContainer);
        scrollviewInnerContainerView.SetBackgroundColor (Android.Graphics.Color.White);//Linear Layout

        var silhouteeContainer = scrollviewInnerContainerView.FindViewById (Resource.Id.view1);//View

        Button b1 = new Button (this);
        b1.SetText ("Click Me");

        LinearLayout.LayoutParams prm = new LinearLayout.LayoutParams (LinearLayout.LayoutParams.MatchParent,LinearLayout.LayoutParams.MatchParent);

     // I want to achieve something like this
       silhouteeContainer.AddView(b1); //when i actually do this , it shows an error.

        return view;
    }
}

And here's what the fChallengeFeedbackView.axml looks ike.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/feedbackView"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/scrollView1">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/scrollViewInnerContainer">
            <View
                android:layout_width="fill_parent"
                android:layout_height="275dp"
                android:id="@+id/view1"
                android:background="#00ff00" />
            <View
                android:layout_width="fill_parent"
                android:layout_height="275dp"
                android:id="@+id/view2"
                android:background="#ff0000" />
            <View
                android:layout_width="fill_parent"
                android:layout_height="275dp"
                android:id="@+id/view3"
                android:background="#0000ff" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

The problem is that you cannot add child views to something which inherits from View . In order to do this the type must inherit from ViewGroup . Such layouts are LinearLayout , FrameLayout and RelativeLayout .

So adding elements to your three declared View instances in your AXML file, is not possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM