我正在以编程方式添加一个文本视图并希望在其上放置一个背景,但我似乎无法让背景居中。(更新)
FrameLayout mTableView1 = new FrameLayout(this);
FrameLayout mTableView2 = new FrameLayout(this);
FrameLayout mTableView3 = new FrameLayout(this);
int bgTop = getResources().getIdentifier("tableb1" , "drawable", this.getPackageName());
int bgMid = getResources().getIdentifier("tableb2" , "drawable", this.getPackageName());
int bgBot = getResources().getIdentifier("tableb3" , "drawable", this.getPackageName());
setTextStyle(tv1, "Dashboard", Gravity.CENTER, mLayoutView, 40, getResources().getColor(R.color.white), getResources().getColor(R.color.transparent), 0, 5);
setTextStyle(tv2, "Happening Now:", Gravity.LEFT, mLayoutView, 15, getResources().getColor(R.color.white), getResources().getColor(R.color.transparent), 0, 5);
setTableStyle(tv3, "Mark Novak Scheduled", Gravity.CENTER, mTableView1, 20, getResources().getColor(R.color.black), getResources().getColor(R.color.transparent), 0, 0, bgTop);
setTableStyle(tv4, "Attendance up 3%", Gravity.CENTER, mTableView2, 20, getResources().getColor(R.color.black), getResources().getColor(R.color.transparent), 0, 0, bgMid);
setTableStyle(tv5, "Location of next event", Gravity.CENTER, mTableView3, 20, getResources().getColor(R.color.black), getResources().getColor(R.color.transparent), 0, 0, bgBot);
mLayoutView.addView(mTableView1);
mLayoutView.addView(mTableView2);
mLayoutView.addView(mTableView3);
}
private void setTextStyle(TextView tv, String s, int g, LinearLayout L, int fsize, int textColor, int backgroundColor, int lpad, int tpad){
tv.setText(s);
tv.setGravity(g);
tv.setTextColor(textColor);
tv.setTextSize(fsize);
tv.setPadding(lpad, tpad, lpad, 0);
L.addView(tv);
}
private void setTableStyle(TextView tv, String s, int g, FrameLayout L, int fsize, int textColor, int backgroundColor, int lpad, int tpad, int bg){
tv.setText(s);
tv.setGravity(g);
tv.setTextColor(textColor);
tv.setTextSize(fsize);
tv.setPadding(lpad, 0, 0, 0);
tv.setBackgroundResource(bg);
tv.setWidth(200);
L.addView(tv);
}
我得到的是文本偏离中心 200 像素,但整个文本视图不是。背景图像从一端延伸到另一端。有没有解决的办法?我试过设置宽度。我让它工作了一点,但后来出了点问题,它又伸出了。
有什么建议么?