Android 以固定像素宽度和高度编程方式创建文本视图

Android 以固定像素宽度和高度编程方式创建文本视图,android,dynamic,textview,Android,Dynamic,Textview,我试过下面的代码 dTextView = new TextView(getContext()); dTextView.setText(item.getText()); dTextView.setTextSize(8); dTextView.setTextColor(Color.BLACK); dTextView.setLayoutParams(new RelativeLayout.LayoutParams((int) measureWidth, (int) measureHeight)); se

我试过下面的代码

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);

不过,它的大小和我想要的不完全一样。这些对于复选框视图也是一样的。我需要用精确的位置和大小来绘制此视图。

为什么需要这样做

您可以创建textview组件,并在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);
}
但是,当我尝试使用此过程创建具有精确宽度和高度的复选框时,它没有创建具有精确宽度和高度的视图。我需要遵循以下代码[需要设置背景并为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);

我有一个PDF文件,该PDF文件有一个空白表单输入,我需要使用动态文本视图或复选框或编辑文本填写这些表单,并且当用户放大或缩小时,它还需要放大。我已经修复了PDF的放大和缩小功能,所以所有视图都保持当前位置,但我需要精确的针点宽度和视图高度。它与TextView一起工作。非常感谢。虽然它不与CheckBox一起使用,(CheckBox没有文本)。有什么解决方案吗?您必须为复选框创建一个单独的params对象,为其提供需要遵守的规则,并以与TextView相同的方式将复选框添加到rootview。按照相同的方式,但不知何故它不起作用,创建具有默认宽度、高度dCheckBox=new CustomCheckBox(getContext())的复选框; CompoundButtonCompat.setButtonTintList(dCheckBox,新的ColorStateList(状态,颜色));dCheckBox.setChecked(item.isCheckState());addView(dCheckBox,新的RelativeLayout.LayoutParams((int)measureWidth,(int)measureHight));您想创建与TextView尺寸相同的复选框…?看到了吗?