【问题标题】:Create TextView programatically with fixed pixel width and height以编程方式创建具有固定像素宽度和高度的 TextView
【发布时间】:2019-08-25 20:29:24
【问题描述】:

我试过下面的代码

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 视图也同样如此。我需要用一个精确的精确位置和大小来绘制这个视图。

【问题讨论】:

    标签: android dynamic textview


    【解决方案1】:

    试试这个方法:

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

    【讨论】:

    • 它适用于 TextView。谢谢你。虽然它不适用于 CheckBox,(CheckBox 没有文本)。有什么解决办法吗?
    • 你必须为 CheckBox 创建一个单独的 params 对象,给它需要遵守的规则,然后像使用 TextView 一样将 CheckBox 添加到 rootview。
    • 遵循相同的方式,但不知何故它不起作用,创建具有默认宽度,高度的 ChecbBox dCheckBox = new CustomCheckBox(getContext()); CompoundButtonCompat.setButtonTintList(dCheckBox, new ColorStateList(states, colors)); dCheckBox.setChecked(item.isCheckState()); rootView.addView(dCheckBox,new RelativeLayout.LayoutParams((int) measureWidth, (int) measureHeight));
    • 您想创建与 TextView 具有相同尺寸的 CheckBox...?
    【解决方案2】:

    为什么你需要这样做。

    您可以创建一个 textview 组件,并借助 ListView、Recyclerview 等以编程方式将其添加到您的 UI 中。

    【讨论】:

    • 我有一个 PDF 文件,该 PDF 文件有一个空白表单输入,我需要使用动态文本视图或复选框或编辑文本来填写这些表单,并且当用户放大或缩小时它还需要按比例放大.我已经修复了 PDF 的放大和缩小,所以所有视图都保持当前位置,但我需要精确的针点宽度和视图高度。
    【解决方案3】:

    最后,我创建了具有精确宽度和高度的所有视图(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);
    

    【讨论】:

      猜你喜欢
      • 2017-02-28
      • 2016-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多