Android - How to programmatically set the button style in a Linearlayout?

前端 未结 6 1785
无人共我
无人共我 2020-12-25 11:22

I am programmatically creating a LinearLayout for an AlertDialog with some buttons.

I WANT to do this:



        
6条回答
  •  执笔经年
    2020-12-25 11:49

    Instead of waqaslams

    LinearLayout button = new LinearLayout(context, null, android.R.style.ButtonBar);
    

    (which is, judging by the comments, API dependable) I have also read ridoys answer from here [ Android Button Styling Programatically ] which is

    transferBtn.setBackgroundResource(R.layout.buttonstyle);
    

    (exposing the above answers because one of these may also work for you, depending of API version you use),

    But this is what worked for me (mind the change from "layout" to "drawable"):

    Button b = new Button(this);
    b.setBackgroundResource(R.drawable.button_custom_green);
    

    (answer from [How to programmatically setting style attribute in a view)

提交回复
热议问题