Layout_below在我的listview中不起作用

时间:2016-07-30 13:19:51

标签: android android-layout listview android-activity android-toolbar

我有自定义工具栏,我希望我的列表视图位于工具栏下方。但是listview出现在工具栏上,而我的layout_below无效。我知道以前有很多问题要问过。但没有任何工作。请帮帮我

下面的

是我的layoutxml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"

tools:context="faisal.home.com.tafaqquhfiddin.DuwaListView">

<include
    layout="@layout/toolbar_layout"
    />

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/MyListView"
    android:layout_below="@+id/toolbar"
   ></ListView>

</RelativeLayout>

toolbarlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.Toolbar    
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/orange"
    app:layout_scrollFlags="scroll|enterAlways"
    app:titleTextColor="@color/white"></android.support.v7.widget.Toolbar>

</LinearLayout>

4 个答案:

答案 0 :(得分:2)

其他用户已经回答了问题......

我只想添加更多信息。

您的布局可以更简单。您不需要使用RelativeLayout。只需使用LinearLayout代替android:orientation="vertical"即可。这样,您将拥有:

layout.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="faisal.home.com.tafaqquhfiddin.DuwaListView">

    <include
        layout="@layout/toolbar_layout"/>

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

</LinearLayout>

工具栏布局

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:layout_scrollFlags="scroll|enterAlways"
    android:background="@color/orange"
    app:titleTextColor="@color/white" />

答案 1 :(得分:1)

LinearLayout中删除toolbarlayout.xml。所以你的toolbarlayout.xml应该是这样的:

<android.support.v7.widget.Toolbar    
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/orange"
    app:layout_scrollFlags="scroll|enterAlways"
    app:titleTextColor="@color/white"></android.support.v7.widget.Toolbar>

答案 2 :(得分:0)

使用以下代码即可。

我的layoutxml

   <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="faisal.home.com.tafaqquhfiddin.DuwaListView">

    <include
        layout="@layout/toolbar_layout"
        />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/MyListView"
        android:layout_below="@+id/toolbar"
       ></ListView>

</LinearLayout >

答案 3 :(得分:0)

toolbarlayout.xml高度的根布局是“match_parent”,将其更改为“wrap_content”。使用下面的代码它将起作用。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="faisal.home.com.tafaqquhfiddin.DuwaListView">

<include
    layout="@layout/toolbar_layout"/>

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/MyListView"
    android:layout_below="@+id/toolbar"/>
</LinearLayout>

<强> toolbarlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.Toolbar    
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/orange"
    app:layout_scrollFlags="scroll|enterAlways"
    app:titleTextColor="@color/white"/>

</LinearLayout>