Make a full view using the ListAdapter

I am trying to make a view in a ListView (via a ListAdapter) with full width. Here is the current test code:

LinearLayout result = new LinearLayout(context); TextView testView = new TextView(context); testView.setText("Aaah"); TextView test2View = new TextView(context); test2View.setText("Eeeeh"); result.addView(testView, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1)); result.addView(test2View, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1)); // To make it easier to see borders result.setBackgroundColor(Color.BLUE); 

(It should be placed in the ListAdapter createView)

Unfortunately, the two TextViews are small and both are stuck on the left. The delimiter is actually the result of the result itself, not occupying the full width, so the two text elements simply share this small space.

Does anyone know how to get them to take full width? Thank you :)


EDIT: simplified (and almost identical) problem:


Code: http://pastebin.com/6pn1hXnT

I want the TextView to be full-sized, and I will center the text so that I can see when it is full and when it only wraps the content. This code shows the text on the left, so MATCH_PARENT does nothing ...

What should I do?

Thanks!

+4
source share
2 answers

I ran into a similar problem, and I followed the advice in this thread. The related topic refers to the ListView inside the dialog box, but in the popup list I observed the same behavior. To make a full-screen TextView, I had to wrap it inside a RelativeLayout:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/ android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="14dip" android:paddingRight="15dip" > <TextView android:id="@+id/list_dialog_item" android:layout_width="fill_parent" android:layout_height="wrap_content" android:minHeight="?android:attr/listPreferredItemHeight" android:textAppearance="?android:attr/textAppearanceLarge" android:gravity="center_vertical" /> </RelativeLayout> 
+4
source

Why don't you add them with fill_parent, but with '0'?

 result.addView(testView, new LinearLayout.LayoutParams(FILL_PARENT , LayoutParams.WRAP_CONTENT, 1)); 
0
source

Source: https://habr.com/ru/post/1335301/


All Articles