简体   繁体   中英

How can i add 2 lines of text to a listView?

i'm having problem trying to achieve two simple things using the listView. I want to lines of text and be able to change the color, new to android but i only found complex ways to achieve this i'm there is a simpler way this my code so far. Thanks.

Activity:

  final ListView list = new ListView(this);
  list.setAdapter(new MyAdapter(this, ficheros));

@Override public View getView(int position, View convertView, ViewGroup parent) {

        RelativeLayout listLayout = new RelativeLayout(OFActivityA.this);


        listLayout.setLayoutParams(new AbsListView.LayoutParams(
                120, 200));
        listLayout.setPadding(0, 0, 0, 0);
        listLayout.setId(View.generateViewId());


        TextView listText = new TextView(OFActivityA.this);
        listText.setId(View.generateViewId());

        listLayout.addView(listText);

        listText.setText(super.getItem(position));

        listLayout.setVisibility(View.VISIBLE);


        return listLayout;
    }

xml

<ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="399dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="193dp"
        android:background="#fefbf8"
        android:text="#fefbf8"
        android:cacheColorHint="#FEFBF8" />

In your custom adapter just do something like:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
    TextView textView1 = (TextView) rowView.findViewById(R.id.tx1);
    TextView textView2 = (TextView) rowView.findViewById(R.id.tx2);
    //...
    return rowView;
}

With this layout rowlayout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView 
        android:id="@+id/tx1"
        .../>

    <TextView 
        android:id="@+id/tx2"
        .../>

</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM