C# 是否可以在运行时填充Android活动?

C# 是否可以在运行时填充Android活动?,c#,android,xamarin,C#,Android,Xamarin,虽然典型的过程是使用AXML定义屏幕的内容,但是否可以在运行时填充活动 大概是这样的: override void OnCreate(Bundle bundle){ base.OnCreate(bundle); // INSTEAD OF THIS: var toolbar = FindViewById<Toolbar>(....); // CAN I DO THIS? var toolbar = New<Toolbar>(..

虽然典型的过程是使用AXML定义屏幕的内容,但是否可以在运行时填充活动

大概是这样的:

override void OnCreate(Bundle bundle){
    base.OnCreate(bundle);

    // INSTEAD OF THIS:
    var toolbar = FindViewById<Toolbar>(....);

    // CAN I DO THIS?
    var toolbar = New<Toolbar>(....);
 }
override void OnCreate(Bundle){
base.OnCreate(bundle);
//与此相反:
var toolbar=findviewbyd(..);
//我能做这个吗?
变量工具栏=新建(…);
}

以下是帮助您入门的示例代码

在XAMARIN中使用C#

var layout = new LinearLayout (this);
layout.Orientation = Orientation.Vertical;

var aLabel = new TextView (this);
aLabel.Text = "Hello, World!!!";

var aButton = new Button (this);
aButton.Text = "Say Hello!";

aButton.Click +=(sender, e) => 
{aLabel.Text="Hello Android!";};

layout.AddView (aLabel);
layout.AddView (aButton);
SetContentView (layout);  
在ANDROID STUDIO中使用JAVA

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.LinearLayout.LayoutParams;

public class DynamicLayoutActivity extends Activity implements OnClickListener{

private static final int MY_BUTTON = 9000;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout2);

    // add text view
    TextView tv = new TextView(this);
    tv.setText("Dynamic Text!");
    ll.addView(tv);

    // add edit text
    EditText et = new EditText(this);
    et.setText("Dynamic EditText!");
    et.setMinLines(1);
    et.setMaxLines(3);
    ll.addView(et);

    // add button
    Button b = new Button(this);
    b.setText("Button added dynamically!");
    b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    b.setId(MY_BUTTON);
    b.setOnClickListener(this);
    ll.addView(b);

    //add checkboxes
    for(int i = 0; i < 10; i++) {
        CheckBox cb = new CheckBox(this);
        cb.setText("Dynamic Checkbox " + i);
        cb.setId(i+10);
        ll.addView(cb);
    }

    //add radio buttons
    final RadioButton[] rb = new RadioButton[5];
    RadioGroup rg = new RadioGroup(this); //create the RadioGroup
    rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
    for(int i=0; i<5; i++){
        rb[i]  = new RadioButton(this);
        rb[i].setText("Dynamic Radio Button " + i);
        rb[i].setId(i);
        rg.addView(rb[i]); //the RadioButtons are added to the radioGroup instead of the layout

    }
    ll.addView(rg);//you add the whole RadioGroup to the layout

    // add Toggle button
    ToggleButton tb = new ToggleButton(this);
    tb.setTextOn("Dynamic Toggle Button - ON");
    tb.setTextOff("Dynamic Toggle Button - OFF");
    tb.setChecked(true);
    tb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    ll.addView(tb);

}

public void onClick(View v) {
    Toast toast;
    Log.w("ANDROID DYNAMIC VIEWS:", "View Id: " + v.getId());
    switch (v.getId()) {
    case MY_BUTTON:
        toast = Toast.makeText(this, "Clicked on my dynamically added button!", Toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP, 25, 400);
        toast.show();
        saveAnswers();
        break;
        // More buttons go here (if any) ...

    }
}

public void saveAnswers() {
    LinearLayout root = (LinearLayout) findViewById(R.id.linearLayout1); //or whatever your root control is
    loopQuestions(root);
}

private void loopQuestions(ViewGroup parent) {
    for(int i = 0; i < parent.getChildCount(); i++) {
        View child = parent.getChildAt(i);
        if(child instanceof RadioGroup ) {
            //Support for RadioGroups
            RadioGroup radio = (RadioGroup)child;
            storeAnswer(radio.getId(), radio.getCheckedRadioButtonId());
        }
        else if(child instanceof CheckBox) {
            //Support for Checkboxes
            CheckBox cb = (CheckBox)child;
            int answer = cb.isChecked() ? 1 : 0;
            storeAnswer(cb.getId(), answer);
        }
        else if(child instanceof EditText) {
            //Support for EditText
            EditText et = (EditText)child;
            Log.w("ANDROID DYNAMIC VIEWS:", "EdiText: " + et.getText());
        }
        else if(child instanceof ToggleButton) {
            //Support for ToggleButton
            ToggleButton tb = (ToggleButton)child;
            Log.w("ANDROID DYNAMIC VIEWS:", "Toggle: " + tb.getText());
        }
        else {
            //Support for other controls
        }

        if(child instanceof ViewGroup) {
            //Nested Q&A
            ViewGroup group = (ViewGroup)child;
            loopQuestions(group);
        }
    }
}

private void storeAnswer(int question, int answer) {
    Log.w("ANDROID DYNAMIC VIEWS:", "Question: " + String.valueOf(question) + " * "+ "Answer: " + String.valueOf(answer) );

    Toast toast = Toast.makeText(this, String.valueOf(question) + " * "+ "Answer: " + String.valueOf(answer), Toast.LENGTH_LONG);
    toast.setGravity(Gravity.TOP, 25, 400);
    toast.show();


}

}
导入android.app.Activity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Gravity;
导入android.view.view;
导入android.view.ViewGroup;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.CheckBox;
导入android.widget.EditText;
导入android.widget.LinearLayout;
导入android.widget.RadioButton;
导入android.widget.RadioGroup;
导入android.widget.TextView;
导入android.widget.Toast;
导入android.widget.ToggleButton;
导入android.widget.LinearLayout.LayoutParams;
公共类DynamicClayOutActivity扩展了活动实现OnClickListener{
专用静态最终int MY_按钮=9000;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout ll=(LinearLayout)findViewById(R.id.LinearLayout 2);
//添加文本视图
TextView tv=新的TextView(此);
tv.setText(“动态文本!”);
ll.addView(电视);;
//添加编辑文本
EditText et=新的EditText(本);
et.setText(“动态编辑文本!”);
et.setMinLines(1);
et.setMaxLines(3);
ll.addView(et);
//添加按钮
按钮b=新按钮(此按钮);
b、 setText(“动态添加的按钮!”);
b、 setLayoutParams(新的LayoutParams(LayoutParams.WRAP_内容,LayoutParams.WRAP_内容));
b、 setId(我的按钮);
b、 setOnClickListener(此);
ll.addView(b);
//添加复选框
对于(int i=0;i<10;i++){
复选框cb=新复选框(此复选框);
cb.setText(“动态复选框”+i);
cb.setId(i+10);
ll.addView(cb);
}
//添加单选按钮
最终单选按钮[]rb=新单选按钮[5];
RadioGroup rg=新的RadioGroup(this);//创建RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);//或RadioGroup.VERTICAL

对于(inti=0;iYes),您可以这样做,但最好尽可能使用axml编写所有UI内容,以便将接口代码与逻辑分离。