简体   繁体   English

Android ListView不显示任何内容(相对布局)

[英]Android ListView doesn't display anything (Relative Layout)

I am having problems with ListView. 我在使用ListView时遇到问题。 It doesn't display. 它不显示。 I know it's a common problem but I'm new to Android. 我知道这是一个常见问题,但是我是Android新手。 In most examples, the problem is solved with layout_bellow property but I do not have any other controls. 在大多数示例中,可以使用layout_bellow属性解决问题,但是我没有任何其他控件。 Here is my XML code: 这是我的XML代码:

Here is my activity_main.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>

And here is my MainActivity.java: 这是我的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);
}
}

Change your ListView to use all width and height of your parent RelativeLayout 更改您的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>

By the way as recommended @Erik Jhordan Rey Caffrey use RecyclerView 按照推荐的方式@Erik Jhordan Rey Caffrey使用RecyclerView

EDIT: If you have any issues here is a good sample to implement a simple listView inside a RelativeLayout 编辑:如果您有任何问题, 这里是一个很好的示例,以在RelativeLayout内部实现一个简单的listView

Hope this helps! 希望这可以帮助!

As your ListView is android:layout_alignParentBottom="true" it stick to bottom with android:layout_height="wrap_content" 由于您的ListViewandroid:layout_alignParentBottom="true"因此它会以android:layout_height="wrap_content" android:layout_alignParentBottom="true"在底部

So make ListView like this 因此,使ListView这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM