【问题标题】:Layout_below is not working in my listviewLayout_below 在我的列表视图中不起作用
【发布时间】:2016-07-30 13:19:51
【问题描述】:

我有自定义工具栏,我希望我的列表视图位于 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>

工具栏布局.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>

【问题讨论】:

  • 问题是因为您在toolbarlayout.xml 上的线性布局占据了屏幕的所有高度,因为您将其指定为匹配父项

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


【解决方案1】:

问题已被其他用户回答...

我只是想补充更多信息。

您的布局可以简单得多。你不需要使用RelativeLayout。只需使用 LinearLayoutandroid: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" />

【讨论】:

【解决方案2】:

从您的toolbarlayout.xml 中删除LinearLayout。所以你的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>

【讨论】:

    【解决方案3】:

    使用下面的代码就可以了。

    我的布局xml

       <?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 >
    

    【讨论】:

      【解决方案4】:

      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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-19
        • 2011-07-29
        • 2015-08-05
        • 2020-08-28
        相关资源
        最近更新 更多