[Android]ListView剪切TextView文本

[Android]ListView cuts TextView text

在我的项目中,我有一个 Activity,它带有一个 ListView 和一个显示自定义项目列表布局的 BaseAdapter。 这个列表项布局有一个文本视图,如果超过一行,他的文本就会被截断。 我写的代码是这样的:

Activity的onCreate方法:

lv = (ListView) findViewById(R.id.listView3);
listaCompleta = new ArrayList<String>();
listaCompleta.add ("Item1");
listaCompleta.add ("Example text");
listaCompleta.add ("This is an example text for writing more than a line in the listview item row blablabla blablabla blablabla");
adapter = new BaseAdapter () {

        @Override
        public int getCount() {
            return listaCompleta.size();
        }

        @Override
        public Object getItem(int position) {
            return listaCompleta.get(position);
        }

        @Override
        public long getItemId(int position) {
            return (long)position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = getLayoutInflater().inflate(R.layout.item_list_layout, null);
            TextView tv1 = (TextView) view.findViewById(R.id.testoItemList);
            tv1.setText(listaCompleta.get(position));

            return view;
        }

    };
lv.setAdapter(adapter);

item_list_layout.xml是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/item_list_layout">

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/testoItemList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"/>

<CheckBox
    android:id="@+id/checkBoxItemList"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:button="@drawable/checkbox_selector"/>

而checkbox_selector.xml是这样的:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/star" android:state_checked="false"/>
<item android:drawable="@drawable/star_active" android:state_checked="true"/>
<item android:drawable="@drawable/star"/>

网上找了很多post跟我的一样,但是都没有解决办法。 我该如何解决这个问题?

试试这个。更改

android:layout_height="match_parent"

android:layout_height="wrap_content"

例如:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/testoItemList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"/>

尝试用这个

替换您的 TextView
<TextView 
    android:id="@+id/testoItemList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:singleLine="false"
    android:gravity="center_vertical"/>

我认为您应该更改文本视图中的 layour_heigth 和 wrap_content 的线性布局。如果 textview 有更多行 de container 展开以调整。我选择match_parent列表视图设置的容器大小

您更改此行:

View view = getLayoutInflater().inflate(R.layout.item_list_layout, null);

为此:

View view = getLayoutInflater().inflate(R.layout.item_list_layout, parent,false);

或者忽略布局参数。

这个很好用