首页 > 解决方案 > TextView 未以编程方式添加到我的布局中

问题描述

我正在尝试以编程方式将 TextViews 添加到 LinearLayout。TextViews 的数量基于他可以使用 AlertDialog Builder 输入的 UserInput 但 TextViews 不会添加到布局中。我不知道为什么。这是我的整个代码。我的代码有什么问题?

public class HandleTableClick extends AppCompatActivity {
public int personsnumber;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.handle_table_click);
    final LinearLayout myLayout = (LinearLayout) findViewById(R.id.myLayout);
    final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Type number of persons");
    final EditText input = new EditText(this);
    input.setInputType(InputType.TYPE_CLASS_NUMBER);
    input.setRawInputType(Configuration.KEYBOARD_12KEY);
    String persons = input.getText().toString();
    try {
        personsnumber = Integer.parseInt(persons);
    }
    catch (NumberFormatException nfe){

    }
    alert.setView(input);
    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            TextView[] pairs=new TextView[personsnumber];
            for(int l=0; l<personsnumber; l++)
            {
                pairs[l] = new TextView(HandleTableClick.this);
                pairs[l].setTextSize(15);
                pairs[l].setLayoutParams(lp);
                pairs[l].setId(l);
                pairs[l].setText((l + 1) + ": something");
                myLayout.addView(pairs[l]);
            }
        }
    });

    alert.show();


}
}

标签: androidlayout

解决方案


我已经测试过了!并且功能完美!:) 我添加了 setWeightSum 来分发 textView

public class MainActivity extends AppCompatActivity {

        TextView textView;
        Integer personsnumber;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            personsnumber=0;
            final LinearLayout myLayout = (LinearLayout) findViewById(R.id.myLayout);
            final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,1);

            AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle("Type number of persons");
            final EditText input = new EditText(this);
            input.setInputType(InputType.TYPE_CLASS_NUMBER);
            input.setRawInputType(Configuration.KEYBOARD_12KEY);
            alert.setView(input);
            alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    String persons = input.getText().toString();
                    try {
                        personsnumber = Integer.parseInt(persons);
                    }
                    catch (NumberFormatException nfe){
                    }
                    myLayout.setWeightSum(personsnumber);
                    for(int l=0; l<personsnumber; l++)
                    {
                        textView= new TextView(MainActivity.this);
                        textView.setTextSize(15);
                        textView.setId(l);
                        textView.setText((l + 1) + ": something");
                        textView.setLayoutParams(lp);
                        myLayout.addView(textView);

                    }
                    dialog.dismiss();
                }
            });
            alert.show();
        }


        }

推荐阅读