以编程方式添加的按钮不显示文本

Programmatically Added Buttons not showing Text

我有一个应用程序,我在其中以编程方式向我的布局添加按钮以确保间距正确。在那个方法中,我使用 setText 方法来更改它们的文本。但是,该文本未显示。 Here's what's being created

这是我使用的代码:

public void addButtons() {
    Button update = new Button(this);
    RelativeLayout.LayoutParams updateParams = new RelativeLayout.LayoutParams(size.x / 5, 200);
    updateParams.addRule(RelativeLayout.CENTER_VERTICAL);
    update.setLayoutParams(updateParams);
    update.setPadding(padding, 0, 0, 0);
    update.setText("Update Me!");
    update.setTextColor(Color.BLUE);
    update.setOnClickListener(new ClickHandler("install_install", this));
    updateId = update.generateViewId();
    update.setId(updateId);
    layout.addView(update);

    Button install = new Button(this);
    RelativeLayout.LayoutParams installParams = new RelativeLayout.LayoutParams(size.x / 5, 200);
    installParams.addRule(RelativeLayout.RIGHT_OF, update.getId());
    installParams.addRule(RelativeLayout.CENTER_VERTICAL);
    install.setLayoutParams(installParams);
    install.setPadding(padding, 0, 0, 0);
    install.setText("Install LastBox");
    install.setTextColor(Color.BLUE);
    install.setOnClickListener(new ClickHandler("lastbox_install", this));
    installId = install.generateViewId();
    install.setId(installId);
    layout.addView(install);

    Button dlFamily = new Button(this);
    RelativeLayout.LayoutParams dlFamilyParams = new RelativeLayout.LayoutParams(size.x / 5, 200);
    dlFamilyParams.addRule(RelativeLayout.RIGHT_OF, install.getId());
    dlFamilyParams.addRule(RelativeLayout.ABOVE, install.getId());
    dlFamily.setLayoutParams(dlFamilyParams);
    dlFamily.setPadding(padding, 0, 0, padding);
    dlFamily.setText("Download Family Build");
    dlFamily.setTextColor(Color.BLUE);
    dlFamily.setOnClickListener(new ClickHandler("family_build", this));
    dlFamilyId = dlFamily.generateViewId();
    dlFamily.setId(dlFamilyId);
    layout.addView(dlFamily);

    Button upFamily = new Button(this);
    RelativeLayout.LayoutParams upFamilyParams = new RelativeLayout.LayoutParams(size.x / 5, 200);
    upFamilyParams.addRule(RelativeLayout.RIGHT_OF, install.getId());
    upFamilyParams.addRule(RelativeLayout.BELOW, install.getId());
    upFamily.setLayoutParams(upFamilyParams);
    upFamily.setPadding(padding, padding, 0, 0);
    upFamily.setText("Update Family Build");
    upFamily.setTextColor(Color.BLUE);
    upFamily.setOnClickListener(new ClickHandler("family_update", this));
    upFamilyId = upFamily.generateViewId();
    upFamily.setId(upFamilyId);
    layout.addView(upFamily);

    Button dlAdult = new Button(this);
    RelativeLayout.LayoutParams dlAdultParams = new RelativeLayout.LayoutParams(size.x / 5, 200);
    dlAdultParams.addRule(RelativeLayout.RIGHT_OF, dlFamily.getId());
    dlAdultParams.addRule(RelativeLayout.ABOVE, install.getId());
    dlAdult.setLayoutParams(dlAdultParams);
    dlAdult.setPadding(padding, 0, 0, padding);
    dlAdult.setText("Download Adult Build");
    dlAdult.setTextColor(Color.BLUE);
    dlAdult.setOnClickListener(new ClickHandler("adult_build", this));
    dlAdultId = dlAdult.generateViewId();
    dlAdult.setId(dlAdultId);
    layout.addView(dlAdult);

    Button upAdult = new Button(this);
    RelativeLayout.LayoutParams upAdultParams = new RelativeLayout.LayoutParams(size.x / 5, 200);
    upAdultParams.addRule(RelativeLayout.RIGHT_OF, upFamily.getId());
    upAdultParams.addRule(RelativeLayout.BELOW, install.getId());
    upAdult.setLayoutParams(upAdultParams);
    upAdult.setPadding(padding, padding, 0, 0);
    upAdult.setText("Update Adult Build");
    upAdult.setTextColor(Color.BLUE);
    upAdult.setOnClickListener(new ClickHandler("adult_update", this));
    upAdultId = upAdult.generateViewId();
    upAdult.setId(upAdultId);
    layout.addView(upAdult);
}

只要您正确声明布局,您的代码就没有问题这里是您项目的示例代码库

LinearLayout layout;
     layout = (LinearLayout) findViewById(R.id.mainLinear);
    layout.setOrientation(LinearLayout.VERTICAL);

现在 addButtons 方法

public void addButtons() {
        Button update = new Button(this);
        RelativeLayout.LayoutParams updateParams = new RelativeLayout.LayoutParams(100, 200);
        updateParams.addRule(RelativeLayout.CENTER_VERTICAL);
        update.setLayoutParams(updateParams);
        update.setPadding(0, 0, 0, 0);
        update.setText("Update Me!");
        update.setTextColor(Color.BLUE);
        update.setOnClickListener(new ClickHandler("install_install", this));
        updateId = update.generateViewId();
        update.setId(updateId);
        layout.addView(update);
    }

我发现在按钮上设置文字大小会使文字显示出来。我猜它默认为 0 号?