Android系统。材料设计按钮以编程方式

时间:2016-02-01 12:58:24

标签: android material-design android-button

我想要显示这样的材质设计按钮:

enter image description here

我正在使用MaterialDesignLibrary。在XML中,我可以用这种方式显示它:

<com.gc.materialdesign.views.ButtonRectangle
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#1E88E5"
                android:text="Button" />

如何以编程方式显示此按钮

1 个答案:

答案 0 :(得分:0)

更新:

首先扩展项目中的类:

public class MyButtonRectangle extends ButtonRectangle {

    public MyButtonRectangle(Context context) {
        super(context, null); //Pass null for AttributeSet Params
    }
}

比在Android中的任何其他视图创建新的MyButtonRectangle:

MyButtonRectangle btn = new MyButtonRectangle(this);
btn.setId(R.id.someId); // you need to add dummy id in ids.xml in values folder.
btn.setText("Button");
btn.setBackgroundColor(getResources().getColor(R.color.colorName));

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); // Replace LinearLayout with your base layout like RelativeLayout,  FrameLayout,  etc.

btn.setLayoutParams(lp);
相关问题