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

时间:2017-01-06 20:33:03

标签: android android-studio settext

我有一个应用程序,我可以通过编程方式在我的布局中添加按钮,以确保间距正常工作。在那种方法中,我使用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);
}

2 个答案:

答案 0 :(得分:0)

只要您在此处正确声明布局,您的代码就可以了,这是您项目的示例代码基础

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);
    }

答案 1 :(得分:0)

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