【问题标题】:How can I add an ImageView as a ListView Header?如何将 ImageView 添加为 ListView 标头?
【发布时间】:2012-07-18 06:56:07
【问题描述】:

谁能帮我找出如何将 ImageView 作为标题添加到 ListView 中?

我的代码在这里:

LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View snapshot = inflater.inflate(R.layout.map_snapshot, null);

ListView channelsList = (ListView) findViewById(R.id.channelsList);
channelsList.addHeaderView(snapshot);

现在它什么也没显示。

【问题讨论】:

    标签: android listview header


    【解决方案1】:

    尝试扩展您的标题视图,将父视图 (ListView) 作为参数传递:

    ListView channelsList = (ListView) findViewById(R.id.channelsList);
    View snapshot = inflater.inflate(R.layout.map_snapshot, channelList, false);
    

    然后,只需将其添加到 ListView:

    channelsList.addHeaderView(snapshot);
    

    【讨论】:

      【解决方案2】:

      试试:

      ListView channelsList = (ListView) findViewById(R.id.channelsList);
      LayoutInflater inflater = getLayoutInflater();
      ViewGroup header = (ViewGroup)inflater.inflate(R.layout.map_snapshot, channelsList , false);
      channelsList .addHeaderView(header, null, false);
      

      【讨论】:

      • 是的,在膨胀时将列表指定为父级有帮助!
      • 我知道,但是我在这里的声誉太低,无法投票回答((真的很抱歉
      【解决方案3】:

      在布局 map_snapshot 中,您必须具有父布局,因为这里我采用了线性布局,同样采用了该布局。

       ListView channelsList = (ListView) findViewById(R.id.channelsList);
          LinearLayout headerView = (LinearLayout) getLayoutInflater().inflate(R.layout.map_snapshot, null);
          listView.addHeaderView(headerView);
      

      如果你想在布局中添加视图:

      TextView mUsername=(TextView) headerView.findViewById(R.id.user_name);
      

      【讨论】:

        【解决方案4】:

        您可以使用 CommonsWare MergeAdapterSectionedListAdapter。有了这个库,您可以在Listview 中使用任何View 作为标题。有关更多信息,请参阅 this SO question 和接受的答案。

        【讨论】:

        • @Downvoter 拒绝投票而不给出理由对任何人都没有帮助。我想知道我做错了什么,我使用了我发布的内容,它真的帮助了我。之前没有测试过,我不会给出答案。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-11
        • 1970-01-01
        • 2018-10-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多