【问题标题】:Android ListView doesn't display anything (Relative Layout)Android ListView 不显示任何内容(相对布局)
【发布时间】:2016-02-19 15:20:08
【问题描述】:

我在使用 ListView 时遇到问题。它不显示。我知道这是一个常见问题,但我是 Android 新手。在大多数示例中,问题已通过 layout_bellow 属性解决,但我没有任何其他控件。 这是我的 XML 代码:

这是我的activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.gkmicro.trrrrrrrrrrrrrr.MainActivity"
    tools:showIn="@layout/activity_main">

    <ListView
        android:id="@+id/myListView"        
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

这是我的 MainActivity.java:

package com.gkmicro.trrrrrrrrrrrrrr;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

【问题讨论】:

  • 您是否在列表中添加任何项目?
  • 张贴Adapter 代码。
  • 您是否指定了一些行布局?我的意思是,你想展示 2 个 TextView 类似的东西吗?如果是这样,请发布您的适配器和适配器调用,以查看您是否正确传递数据
  • 你为什么使用 ListView? RecyclerView 是要走的路,所以也许你可以看看
  • 我推荐使用 RecyclerView! erikcaffrey.github.io/2015/10/05/recyclerview

标签: android android-layout android-listview android-relativelayout


【解决方案1】:

由于您的ListViewandroid:layout_alignParentBottom="true",它与android:layout_height="wrap_content" 保持一致

所以让ListView 像这样

<ListView
    android:id="@+id/myListView"        
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
     />

【讨论】:

  • 谢谢。我试过了,结果还是一样。
【解决方案2】:

更改您的 ListView 以使用您父母 RelativeLayout 的所有宽度和高度

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.gkmicro.trrrrrrrrrrrrrr.MainActivity"
    tools:showIn="@layout/activity_main">

    <ListView
        android:id="@+id/myListView"        
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

顺便推荐@Erik Jhordan Rey Caffrey 使用RecyclerView

编辑:如果您有任何问题 here is a good sample 在 RelativeLayout 中实现简单的 listView

希望这会有所帮助!

【讨论】:

  • 不幸的是,它没有帮助。
  • 你还有同样的问题吗?
  • 不幸的是,是的。我已经尝试了所有选项,但结果仍然相同。
  • 请发布您的适配器
  • 我不确定您所说的适配器是什么意思,但我已经提交了 activitymain.xml 和 MainActivity.java 文件。请看一看。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-10
  • 1970-01-01
  • 1970-01-01
  • 2013-08-02
  • 1970-01-01
  • 1970-01-01
  • 2011-05-25
相关资源
最近更新 更多