【问题标题】:How can I add text multiple text views to the list view items in android? [closed]如何将文本多个文本视图添加到 android 中的列表视图项? [关闭]
【发布时间】:2016-02-25 17:16:48
【问题描述】:

如何在android中的列表视图项中添加文本多个文本视图?请添加代码

【问题讨论】:

  • 无法理解您的问题。需要更多细节
  • “请添加代码” - 请添加您的。
  • @MathaN .........我有一个列表视图和一个名为 Quantity 的列表视图项。我需要根据数量值显示文本视图。如果数量 = 2 然后需要显示 2 个文本视图
  • 查看可能适合您问题的答案。

标签: android


【解决方案1】:

您必须通过自定义布局覆盖默认列表视图项。 在onCreate() 使用:

    yourList = (ListView) findViewById(R.id.yourList);
arrayAdapter = new MyClassAdapter(this,android.R.layout.simple_list_item_1, results);
    yourList.setAdapter(arrayAdapter);

public class MyClassAdapter extends ArrayAdapter<Long> {

覆盖这个:

 public View getView(int position, View convertView, ViewGroup list) {
View element;
                if (convertView == null) {
                    element = View.inflate(ctx, R.layout.yourlayout, null);
                } else {
                    element = convertView;
                }
///setup your listview element
return element;
    }

res/layout/yourlayout.xml 文件中创建如下内容:

 <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/txt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
<TextView
            android:id="@+id/txt2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
 </LinearLayout>

更新res/layout/yourlayout.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

     </LinearLayout>

onCreate():arrayAdapter.setQ(2);

public class MyClassAdapter extends ArrayAdapter&lt;Long&gt; {

public class MyClassAdapter extends ArrayAdapter<Long> {
private int q = 2;
public void setQ(int num){
this.q = num;
}

 public View getView(int position, View convertView, ViewGroup list) {
    ViewGroup element;
                    if (convertView == null) {
                        element = (ViewGroup) View.inflate(ctx, R.layout.yourlayout, null);
                    } else {
                        element = (ViewGroup) convertView;
                    }
element.removeAllViews();
for (int i = 0; i < q; i++){
TextView tv = new TextView(this);
tv.setText("blablabla");
element.addChild(tv);
}
    ///setup your listview element
    return element;
}

【讨论】:

  • 我还没有得到解决方案?
【解决方案2】:

膨胀你的 ListView 并在你的 ListItem 视图中添加你想要的任意数量的 TextView。

这里查看reference & sample的示例

【讨论】:

    【解决方案3】:

    创建如下所示的适配器布局

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/headerlayoutid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="Vertical">
    </LinearLayout>
    

    在您的适配器页面中,使用以下代码创建动态文本视图。

     final TextView t = new TextView(this);
        t.setText("String value");
        t.setTextSize(14);
    //You can set gravity for the textview
        t.setGravity(Gravity.CENTER_VERTICAL);
        LinearLayout ll = (LinearLayout) findViewById(R.id.headerlayoutid);
       ll.addView(t);
    

    如果您需要很多文本视图,请使用 for 循环中的代码。够了

    【讨论】:

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