当前位置: 代码迷 >> Android >> 在AutoCompleteTextView的元素上添加textview单击
  详细解决方案

在AutoCompleteTextView的元素上添加textview单击

热度:120   发布时间:2023-08-04 12:48:47.0

选择AutoCompleteTextView的元素时,我无法添加textview。 您可以在此代码中找到问题吗?

 actv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView tv = new TextView(view.getContext());
                tv.setText("something");
                Log.d("Test","Test");//Works!!
                layout=(LinearLayout) findViewById(R.id.container);
                layout.addView(tv);

            }
 });

这是我的活动布局:

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

<LinearLayout
    android:orientation="vertical"
    android:layout_width="382dp"
    android:layout_height="518dp"
    android:id="@+id/container"
    android:focusableInTouchMode="false">

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView_aff_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/cli_id"
        android:hint="Choisissez un prospect"
        android:ems="10"
        android:text="">
    </AutoCompleteTextView>
</LinearLayout>

您没有为TextView设置任何布局参数,因此基本上您没有告诉它应该如何显示。 尝试这样的事情:

final TextView tv = new TextView(this);

tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));

...
layout.addView(tv);
  相关解决方案