動態生成一個textview,設定padding不起作用的解決辦法。
阿新 • • 發佈:2019-01-02
今天在動態寫佈局的時候,動態new了一個TextView,然後給其設定padding不起作用。
TextView tv = new TextView(mContext); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,50);
tv.setLayoutParams(params);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setText ("一類工具");
tv.setTextSize(16); tv.setTextColor(Color.parseColor("#000000"));
tv.setPadding(5,0,0,0);
tv.setBackgroundResource(R.drawable.layer_textview);
ll_tools_add.addView(tv);
後來,在這裡查到,tv.setPadding()要放在setBackgroundResource()後面。
tv.setBackgroundResource(R.drawable.layer_textview); tv.setPadding(5,0,0,0);
這樣一來就解決了問題。