android listpopupwindow list item textview not being multiline

二次信任 提交于 2019-12-10 10:42:50

问题


I am using ListPopupWindow with custom list item layout. But list item TextView isn't being multi-line. How can I do that?

final ListPopupWindow showRoomListPopupWindow = new ListPopupWindow(
                getActivity());
        showRoomListPopupWindow.setAdapter(new ArrayAdapter(
                getActivity(),
                R.layout.movie_detail_spinner_item, movieShowRoomARList));
        showRoomListPopupWindow.setModal(true);
//        showRoomListPopupWindow.setWidth(ListPopupWindow.MATCH_PARENT);


        showRoomListPopupWindow.setAnchorView(tvProgramSeansClick_TXT);

XML layout:

<?xml version="1.0" encoding="utf-8"?>
<TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:padding="10dp"
        android:textStyle="bold"
        android:minLines="3"
        android:singleLine="false"
        />

回答1:


I tried to recreate your case, and I've managed to get your code working, just doing the following:

My AnchorView is a Button, inside a RelativeLayout, like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
  <Button
    android:id="@+id/popup"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@null"
    android:visibility="invisible"
    android:text="Popup"
    android:textAppearance="@android:attr/textAppearanceSmall" />

Then, I have the following code inside my Activity onCreate():

Button popupButton = (Button) findViewById(R.id.popup);

    ArrayList<String> array = new ArrayList<String>();
    array.add("spendisse vel libero lacinia neque hendrerit posuere nec ac sem. Aliquam laoreet ullamcorper tortor, tincidunt suscipit eros pulvinar dictum. Cras eleifend ante at laoreet facilisis. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridic lorem ipsum");
    array.add("Odio. Quisque at rutrum tellus. Nullam hendrerit nisl non ligula condimentum varius aliquet eget turpis. Cras cursus arcu ornare elit dictum, et aliquet sem condimentum. Praesent mauris mi, malesuada volutpat dictum a, porta non mauris. Cras non sce");
    array.add("Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci veli");
    array.add("ltricies interdum sit amet nec ante. Suspendisse vel dolor dolor. Donec eget mattis lorem. Donec eget quam porta, hendrerit sapien a, interdum arcu. Integer rutrum, ante sed posuere adipiscing, nisl mi malesuada lorem, quis egestas sem augue fauci");

    final ListPopupWindow showRoomListPopupWindow = new ListPopupWindow(SecondActivity.this);
    showRoomListPopupWindow.setAdapter(new ArrayAdapter(SecondActivity.this,
            R.layout.movie_detail_spinner_item, array));
    showRoomListPopupWindow.setModal(true);

    showRoomListPopupWindow.setAnchorView(popupButton);

    showRoomListPopupWindow.setWidth(ListPopupWindow.WRAP_CONTENT);

    showRoomListPopupWindow.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(SecondActivity.this, "Clicked item " + position, Toast.LENGTH_SHORT).show();
        }
    });

    popupButton.post(new Runnable() {
        public void run() {
            showRoomListPopupWindow.show();
        }
    });

Note that all my strings are very long, and my Button has its visibility set to INVISIBLE, not GONE.

And this is what I get:

As you can see, the text has a minimum 3 lines and the width of the items is ok.

I hope to have helped you.

Let me know if it worked for you.



来源:https://stackoverflow.com/questions/23033417/android-listpopupwindow-list-item-textview-not-being-multiline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!