android listview 也在迭代页脚菜单

发布于 2024-12-09 10:32:13 字数 3242 浏览 0 评论 0 原文

这是我的活动代码:

@Override
    public void onCreate(Bundle icicle)
    {
    super.onCreate(icicle);
    String[] names = new String[] { "Investor Relations", "Social Sharing", "Rate this app", "About Rathbones",
            "Disclaimer", "Security", "Credits"};
    // Create an ArrayAdapter, that will actually make the Strings above
    // appear in the ListView
    this.setListAdapter(new ArrayAdapter<String>(this, R.layout.moremenulist,
            R.id.label, names));
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        // Get the item that was clicked
        Object o = this.getListAdapter().getItem(position);
        String keyword = o.toString();
        Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG).show();
    }

这里是 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/LinearLayout1"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <TextView android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:id="@+id/label"></TextView>
    <LinearLayout android:id="@+id/linearLayout2" android:layout_width="wrap_content" android:orientation="vertical" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentBottom="true">
    
          <RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    
        <ImageButton android:layout_width="wrap_content" android:id="@+id/imgbtn1" android:layout_alignParentLeft="true" android:layout_alignBottom="@+id/imgbtn3" android:layout_height="wrap_content" android:src="@drawable/topnews" android:visibility="visible"></ImageButton>
        <ImageButton android:layout_width="wrap_content" android:layout_alignParentRight="true" android:id="@+id/imgbtn5" android:layout_alignBottom="@+id/imgbtn4" android:layout_height="wrap_content" android:src="@drawable/more"></ImageButton>
        <ImageButton android:layout_width="wrap_content" android:id="@+id/imageButton1" android:layout_height="wrap_content" android:src="@drawable/contact_us" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/imgbtn1"></ImageButton>
    
</RelativeLayout>
    </LinearLayout>
        
        
 
</RelativeLayout>

现在,当活动加载时,它会将菜单中的每个项目附加到列表中。我只是希望它只出现在底部,而不是出现在每个列表项中。 有什么建议吗?

Edit-1 :

我尝试了这个,但出现了空指针异常的错误

View footerView = 
        

    ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer, null, false);
        ListView lv = new ListView(this);
        lv.addFooterView(footerView);

This is my activity code:

@Override
    public void onCreate(Bundle icicle)
    {
    super.onCreate(icicle);
    String[] names = new String[] { "Investor Relations", "Social Sharing", "Rate this app", "About Rathbones",
            "Disclaimer", "Security", "Credits"};
    // Create an ArrayAdapter, that will actually make the Strings above
    // appear in the ListView
    this.setListAdapter(new ArrayAdapter<String>(this, R.layout.moremenulist,
            R.id.label, names));
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        // Get the item that was clicked
        Object o = this.getListAdapter().getItem(position);
        String keyword = o.toString();
        Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG).show();
    }

And here is the xml file for this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/LinearLayout1"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <TextView android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:id="@+id/label"></TextView>
    <LinearLayout android:id="@+id/linearLayout2" android:layout_width="wrap_content" android:orientation="vertical" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentBottom="true">
    
          <RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    
        <ImageButton android:layout_width="wrap_content" android:id="@+id/imgbtn1" android:layout_alignParentLeft="true" android:layout_alignBottom="@+id/imgbtn3" android:layout_height="wrap_content" android:src="@drawable/topnews" android:visibility="visible"></ImageButton>
        <ImageButton android:layout_width="wrap_content" android:layout_alignParentRight="true" android:id="@+id/imgbtn5" android:layout_alignBottom="@+id/imgbtn4" android:layout_height="wrap_content" android:src="@drawable/more"></ImageButton>
        <ImageButton android:layout_width="wrap_content" android:id="@+id/imageButton1" android:layout_height="wrap_content" android:src="@drawable/contact_us" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/imgbtn1"></ImageButton>
    
</RelativeLayout>
    </LinearLayout>
        
        
 
</RelativeLayout>

Now, when the activity is loading it is appending the menu with each item in the list. I just want it to appear only in bottom not with each and every list item.
any suggestions?

Edit-1 :

I tried this but getting an error of nullpointer exception

View footerView = 
        

    ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer, null, false);
        ListView lv = new ListView(this);
        lv.addFooterView(footerView);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫卆 2024-12-16 10:32:13

你说的菜单是什么意思?你的意思是你的图像按钮吗?

如果是这种情况,则删除它们,为这些 ImageButtons 创建另一个布局,并使用 ListView.addFooterView() 代替。

您可以创建例如 list_footer_layout.xml,其中包含您的 ImageButtons。然后:

public void onCreate(Bundle bundle) {
    ...
    ListView lv = getListView(); // If your activity is ListActivity
    View foot = getLayoutInflater().inflate(R.layout.list_footer_layout, null);
    lv.addFooterView(foot);
}

What do you mean by menu? Do you mean your ImageButtons?

If that's the case then remove those create another layout for those ImageButtons and add them using ListView.addFooterView() instead.

You could create e.g. list_footer_layout.xml, which contains your ImageButtons. Then:

public void onCreate(Bundle bundle) {
    ...
    ListView lv = getListView(); // If your activity is ListActivity
    View foot = getLayoutInflater().inflate(R.layout.list_footer_layout, null);
    lv.addFooterView(foot);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文