how to settittle to listview in infater layout?

╄→尐↘猪︶ㄣ 提交于 2019-12-04 18:42:44

just remove this:

dialogMarketList.requestWindowFeature(Window.FEATURE_NO_TITLE);

Edited answer:

use this way:

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        viewList = getLayoutInflater().inflate(R.layout.activity_main_, null);
        dialogMarketList = new Dialog(MainActivity.this);
        dialogMarketList.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialogMarketList.setTitle("TOPICS");
        dialogMarketList.setContentView(viewList);
        dialogMarketList.show();
        ListView lvForDialog = (ListView) viewList.findViewById(R.id.listView1);

        header = inflater.inflate(R.layout.header, null);

        lvForDialog.addHeaderView(header);
        lvForDialog.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
        ArrayAdapter<String> adapter = (new ArrayAdapter<String>(
                MainActivity.this, android.R.layout.simple_list_item_1, a));
        lvForDialog.setAdapter(adapter);

header.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="my title"
        android:textAppearance="?android:attr/textAppearanceLarge" />


Edited 2nd time:

avoid title go up when scrolling use below code insted of above.

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("my Title");
        builder.setIcon(getResources().getDrawable(R.drawable.ic_launcher));

        builder.setAdapter(new ArrayAdapter<String>(MainActivity.this,
                R.layout.row, R.id.child_row, items),
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub


                    }
                });

        builder.show();

The following command in your code disables the title in the dialog:

dialogMarketList.requestWindowFeature(Window.FEATURE_NO_TITLE);

If you remove it you should see the dialog with the title.

Primoz990

You are possible want to add a header to listview?

Look add this answer https://stackoverflow.com/a/10730786/1293704

Many tutorials can be found how to add a header.

try to add headerView in your listView, you need to do it before you call setAdapter()

What docs says /

public void addHeaderView (View v)

*Add a fixed view to appear at the top of the list. If addHeaderView is called more than once, the views will appear in the order they were added. Views added using this call can take focus if they want. NOTE: Call this before calling setAdapter. This is so ListView can wrap the supplied cursor with one that will also account for header and footer views.

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