【问题标题】:Align dynamically created textview and edit text in android在android中对齐动态创建的textview并编辑文本
【发布时间】:2016-05-04 20:07:13
【问题描述】:

我有一个相对布局和“n”个 textviews 和 edittext 。我希望对齐方式为

  TextView   EditText
       Button
  TextView   EditText
        Button
   TextView   EditText
         Button
  TextView   EditText
        Button
  ..........

我尝试过使用布局参数,但结果很奇怪。 这是我的代码,

 RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.linearlayoutid);
    final TextView[] myTextViews = new TextView[41];
    for (int i = 0; i < 41; i++) {
        // create a new textview
        rowTextView = new TextView(this);
        RelativeLayout.LayoutParams paramsA = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        paramsA.addRule(RelativeLayout.RIGHT_OF, rowTextView.getId());
        rowTextView.setId(i);
        // add the textview to the layout
        relativeLayout.addView(rowTextView, paramsA);
        myTextViews[i] = rowTextView;

    }

用于编辑文本..

    final EditText[] editTextarray = new EditText[41];
    for (int i = 0; i < 41; i++) {
        // create a new textview
        final EditText editText = new EditText(this); 
        RelativeLayout.LayoutParams paramsB = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
         editText.setText("edit");
        editText.setCursorVisible(true);
        relativeLayout.addView(editText, paramsB);
        editTextarray[i] = editText;

    }

我已经应用了这样的 layout_params 但没有帮助。任何帮助都会很棒!谢谢

【问题讨论】:

  • 这里为什么不用线性布局?
  • 你想让我使用线性布局而不使用参数吗?它是动态生成的。我怎样才能有线性布局?它将动态生成大约 100 个编辑文本@SohailZahid
  • 如果您不使用相对布局属性,那么只需使用带方向的线性布局。
  • 出于性能考虑,您可以在单个循环中添加 Editext 和 TextView。
  • 如果我使用水平方向的线性布局,那么我的输出将类似于 edittext edittext edittext............. @SohailZahid

标签: android alignment layoutparams


【解决方案1】:

添加布局

layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/ll"
        >
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="textview"
            />
        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Edittex"
            />

    </LinearLayout>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ll"
        android:layout_centerHorizontal="true"
        android:text="button"
        />

</RelativeLayout>

在运行时添加此布局:

    View view;
    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

   for (int i = 0; i < 40; i++) {

     view= inflater.inflate(R.layout.layout, null);
     linearLayout.addView(view);
    }

【讨论】:

  • 你能告诉我现在如何将动态值传递给文本视图吗? @Pradeep Gupta
  • 在for循环中添加这个 TextView tvBuy = (TextView) view.findViewById(R.id.textview);tvBuy .setText("your Value");
  • 它在 (LayoutInflator) 表达式中显示错误,预期 @Pradeep Gupta
  • 那线性布局是什么?线性布局的 id? @Pradeep Gupta
  • 我已经编辑了代码,请检查一下,linearlayout 是您的 xml 中的 id。
【解决方案2】:

我建议您使用自定义布局并以编程方式对其进行扩展。

创建一个由 TextViewEditTextButton 组成的布局,并添加所需的输出。

View v=LayoutInflater.from(this).inflate(R.layout.customLayout,null);

并使用变量v 引用TextViewEditTextButton

您的customLayout.xml 可以是这样的:

<LinearLayout android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

在您的 xml 中获取具有垂直方向的 LinearLayout,并将此视图添加到该布局中以创建不同的视图。

for (int i = 0; i < 20; i++) {
    View v = inflater.inflate(R.layout.customLayout, null);

    ll.addView(v);
}

【讨论】:

  • 您能修改我的代码并提供解决方案吗? @Ravi Rupareliya
  • 对不起,不能给你完整的代码,可以给你提示。如果人们对此投反对票,我也会接受。
  • LinearLayout lv = (LinearLayout) findViewById(R.id.linearlayoutid); .我们应该使用 id 访问线性布局吗?你的代码中的ll是引用权吗?
  • 这里 ll 是我的主布局中的 LinearLayout,您将在其中添加所有这些视图。
  • 它只显示一个编辑文本、文本视图和按钮,但不是全部@Ravi Rupareliya
【解决方案3】:

Anusha 用线性布局试试这个

如果你的设计是相同的,你应该使用 listview 最佳方法。

 LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearlayoutid);
        final TextView[] myTextViews = new TextView[41];
        final EditText[] editTextarray = new EditText[41];
        for (int i = 0; i < 41; i++) {
            // create a new textview
            rowTextView = new TextView(this);
            myTextViews[i] = rowTextView;
            linearLayout.addView(myTextViews[i]);

            final EditText editText = new EditText(this);
            editText.setText("edit");
            editText.setCursorVisible(true);
            editTextarray[i] = editText;
            linearLayout.addView(editTextarray[i]);

        }

【讨论】:

    【解决方案4】:

    我建议你为这个需求实现 ListView -

    根据您当前的方法,您最终将为每个按钮设置多个 onClicklistener - 如果该按钮要选择与 textview/edittext 关联的输入数据 - 您最终将编写许多数组调用和东西

    Listview - onclick 任何项目,你可以在你区分的地方触发一个 onClickListener

    PS:如果希望按钮执行相同/类似的功能(例如保存),则在 textview/edittext 下方重复按钮的设计不佳 - 只是有一个 textview/edittext 的列表视图 - 工具栏上某处或表单下方的常见按钮操作。

    【讨论】:

      猜你喜欢
      • 2013-09-10
      • 1970-01-01
      • 2018-07-31
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      • 2021-05-01
      • 2012-02-16
      相关资源
      最近更新 更多