Set style for TextView programmatically

后端 未结 12 1649
南方客
南方客 2020-11-22 09:47

I\'m trying to use the TextView constructor with style like this:

TextView myText = new TextView(MyActivity.this, null, R.style.my_style);
         


        
12条回答
  •  清酒与你
    2020-11-22 10:13

    The accepted answer was great solution for me. The only thing to add is about inflate() method.

    In accepted answer all android:layout_* parameters will not be applied.

    The reason is no way to adjust it, cause null was passed as ViewGroup parent.

    You can use it like this:

    View view = inflater.inflate(R.layout.view, parent, false);

    and the parent is the ViewGroup, from where you like to adjust android:layout_*.

    In this case, all relative properties will be set.

    Hope it'll be useful for someone.

提交回复
热议问题