how to settittle to listview in infater layout?

2020-07-27 05:21发布

I'm new to android..

I'm facing problem with tittle for listview..

How to implement tittle for list view? help me..

I try like this..

      LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      viewList = Nexttopic.this.getLayoutInflater().inflate(R.layout.activity_nexttopic, null);
      dialogMarketList = new Dialog(Nexttopic.this);
      dialogMarketList.requestWindowFeature(Window.FEATURE_NO_TITLE);
      dialogMarketList.setTitle("TOPICS");
      dialogMarketList.setContentView(viewList);
      dialogMarketList.show();     
      lvForDialog = (ListView) viewList.findViewById(R.id.List_view);
      lvForDialog.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
      ArrayAdapter<String> adapter = (new ArrayAdapter<String>(Nexttopic.this, R.layout.row_topic, R.id.child_row,tnamelist));
      lvForDialog.setAdapter(adapter);    

Its not working..

4条回答
疯言疯语
2楼-- · 2020-07-27 05:40

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.

查看更多
太酷不给撩
3楼-- · 2020-07-27 05:49

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.

查看更多
虎瘦雄心在
4楼-- · 2020-07-27 05:52

just remove this:

dialogMarketList.requestWindowFeature(Window.FEATURE_NO_TITLE);

Edited answer:

enter image description here

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();
查看更多
再贱就再见
5楼-- · 2020-07-27 05:58

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.

查看更多
登录 后发表回答