【问题标题】:Apply button style dynamically动态应用按钮样式
【发布时间】:2016-12-05 15:24:42
【问题描述】:

我有一点麻烦: 我将创建应用@style/Widget.AppCompat.Button.Borderless 样式的动态材质按钮。显然,如果我在 xml 文件中将按钮创建为静态按钮,它可以正常工作,但在 java 代码中我不知道如何做到这一点。这是我的代码

  for (String nameCat : cat){
        Button button = new Button(getActivity());
        lP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, dpToPx(60)); // dpToPx is convert method
        button.setLayoutParams(lP);

        // text button
        button.setText(nameCat);
        // add to linear layout
        lL.addView(button);
    }

【问题讨论】:

    标签: android button styles


    【解决方案1】:

    如果你在 XML 中有你的风格

     ContextThemeWrapper newContext = new ContextThemeWrapper(baseContext, R.style.MyStyle);
        button = new Button(newContext);
    

    【讨论】:

    • 谢谢,我现在试试。你知道我在哪里可以找到@style/Widget.AppCompat.Button.Borderless 文件吗?
    猜你喜欢
    • 2013-03-02
    • 1970-01-01
    • 2014-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    • 2020-07-17
    相关资源
    最近更新 更多