以编程方式创建具有固定像素宽度和高度的 TextView
Create TextView programatically with fixed pixel width and height
我试过下面的代码
dTextView = new TextView(getContext());
dTextView.setText(item.getText());
dTextView.setTextSize(8);
dTextView.setTextColor(Color.BLACK);
dTextView.setLayoutParams(new RelativeLayout.LayoutParams((int) measureWidth, (int) measureHeight));
setMargin(dTextView, item);
rootView.addView(dTextView);
不过,这不是我想要的确切尺寸。这些事情对于 CheckBox 视图也是一样的。我需要用精确的精确位置和大小来绘制这个视图。
为什么要这样做。
您可以创建一个文本视图组件,并借助 ListView、Recyclerview 等以编程方式将其添加到您的 UI
试试这个方法:
dTextView = new TextView(getContext());
dTextView.setText(item.getText());
dTextView.setTextSize(8);
dTextView.setTextColor(Color.BLACK);
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams((int) measureWidth, (int)measureHeight));
setMargin(dTextView, item);
rootView.addView(dTextView, params);
最后,我创建了具有精确宽度和高度的所有视图(Checkbox、EditText 和 TextView)。 EditText 和 TextView 是一个简单的解决方案:
dEditText = new EditText(getContext());
dEditText.setPadding(10,5,10,5);
dEditText.setTextSize(6);
dEditText.setTextColor(Color.WHITE);
dEditText.setBackgroundColor(Color.GRAY);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
(int) measureWidth, (int) measureHeight);
dEditText.setLayoutParams(params);
rootView.addView(dEditText);
setMargin(dEditText, item,params);
// set margin of every created view
private void setMargin(View view, TagsItem item,RelativeLayout.LayoutParams layoutParams) {
layoutParams.setMargins(item.getCurrentXPos(), item.getCurrentYPos(), 0, 0);
// Apply the updated layout parameters to TextView
view.setLayoutParams(layoutParams);
}
但是当我尝试使用此过程创建具有精确宽度和高度的 CheckBox 时,它没有创建具有精确宽度和高度的视图。我需要按照下面的代码[需要设置背景并为 setButtonDrawable 设置 null ]
int states[][] = {{android.R.attr.state_checked}, {}};
int colors[] = {Color.BLACK, Color.BLACK};
dCheckBox = new CheckBox(getContext());
CompoundButtonCompat.setButtonTintList(dCheckBox, new ColorStateList(states, colors));
dCheckBox.setChecked(item.isCheckState());
//dCheckBox.setText("");
dCheckBox.setBackgroundResource(R.drawable.check_box_selector);
dCheckBox.setButtonDrawable(null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
(int) measureWidth, (int) measureHeight);
dCheckBox.setLayoutParams(params);
// checkBoxLayout.addView(dCheckBox);
rootView.addView(dCheckBox);
setMargin(dCheckBox, item,params);
我试过下面的代码
dTextView = new TextView(getContext());
dTextView.setText(item.getText());
dTextView.setTextSize(8);
dTextView.setTextColor(Color.BLACK);
dTextView.setLayoutParams(new RelativeLayout.LayoutParams((int) measureWidth, (int) measureHeight));
setMargin(dTextView, item);
rootView.addView(dTextView);
不过,这不是我想要的确切尺寸。这些事情对于 CheckBox 视图也是一样的。我需要用精确的精确位置和大小来绘制这个视图。
为什么要这样做。
您可以创建一个文本视图组件,并借助 ListView、Recyclerview 等以编程方式将其添加到您的 UI
试试这个方法:
dTextView = new TextView(getContext());
dTextView.setText(item.getText());
dTextView.setTextSize(8);
dTextView.setTextColor(Color.BLACK);
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams((int) measureWidth, (int)measureHeight));
setMargin(dTextView, item);
rootView.addView(dTextView, params);
最后,我创建了具有精确宽度和高度的所有视图(Checkbox、EditText 和 TextView)。 EditText 和 TextView 是一个简单的解决方案:
dEditText = new EditText(getContext());
dEditText.setPadding(10,5,10,5);
dEditText.setTextSize(6);
dEditText.setTextColor(Color.WHITE);
dEditText.setBackgroundColor(Color.GRAY);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
(int) measureWidth, (int) measureHeight);
dEditText.setLayoutParams(params);
rootView.addView(dEditText);
setMargin(dEditText, item,params);
// set margin of every created view
private void setMargin(View view, TagsItem item,RelativeLayout.LayoutParams layoutParams) {
layoutParams.setMargins(item.getCurrentXPos(), item.getCurrentYPos(), 0, 0);
// Apply the updated layout parameters to TextView
view.setLayoutParams(layoutParams);
}
但是当我尝试使用此过程创建具有精确宽度和高度的 CheckBox 时,它没有创建具有精确宽度和高度的视图。我需要按照下面的代码[需要设置背景并为 setButtonDrawable 设置 null ]
int states[][] = {{android.R.attr.state_checked}, {}};
int colors[] = {Color.BLACK, Color.BLACK};
dCheckBox = new CheckBox(getContext());
CompoundButtonCompat.setButtonTintList(dCheckBox, new ColorStateList(states, colors));
dCheckBox.setChecked(item.isCheckState());
//dCheckBox.setText("");
dCheckBox.setBackgroundResource(R.drawable.check_box_selector);
dCheckBox.setButtonDrawable(null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
(int) measureWidth, (int) measureHeight);
dCheckBox.setLayoutParams(params);
// checkBoxLayout.addView(dCheckBox);
rootView.addView(dCheckBox);
setMargin(dCheckBox, item,params);