【问题标题】:Start ListView below top of screen在屏幕顶部下方启动 ListView
【发布时间】:2014-12-18 23:59:53
【问题描述】:

我正在尝试在我的ListView 上方添加某种填充或边距,以便我可以添加我自己的带有图像和按钮的自定义导航栏。我尝试向我的 XML 添加填充和边距,但我的 ListView 似乎仍然从顶部开始。

如何开始我的列表视图低于屏幕顶部大约50p 左右?

布局

<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="com.mycompany.myapp.myActivity"
android:padding="0dp">

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/theList"
    android:paddingTop="44dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/imageView" />


</RelativeLayout>

【问题讨论】:

  • 请添加您的布局xml
  • 添加一张你拥有什么和想要什么的图片。

标签: java android xml listview


【解决方案1】:

padding 在应用它的容器边界内添加空间。您想在ListViewRelativeLayout 上使用marginTopmargin 在应用它的容器边界之外添加空间)或paddingTop

对于您的情况,您可能应该使用ToolBar,并将其放在您的ListView 上方。例如,如果您使用Linearlayout 而不是RelativeLayout,则只需给出ToolBar 尺寸就会将ListView 推到其下方。

【讨论】:

  • 我似乎找不到工具栏。它应该在小部件下吗?
  • ToolBar 是在 API 21 中添加的。您可以将它的 support v7 版本用于以前的版本。您需要将compile 'com.android.support:appcompat-v7:21.0.3' 添加到模块的build.gradle
  • 我目前有这个compile 'com.android.support:appcompat-v7:21.0.3'
  • 请记住,当 SDK 本身不包含视图时,当您将视图添加到 XML 层次结构时,您需要完全指定视图的整个包名称。在这种情况下,它将是 &lt;android.support.v7.widget.Toolbar&gt;...&lt;/ android.support.v7.widget.Toolbar&gt;
【解决方案2】:

试试这段代码。 第二个LinearLayout 可用于保存您选择的自定义导航项并让它选择其高度。 列表视图将自动填充视图的其余部分。

类似的东西,试试ListView的margin

<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="com.mycompany.myapp.myActivity">

 <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content">
 <!-- Place your images and buttons need for navigation here -->
 <!-- Also dont forget to choose the appropiate orientation to this layout-->
 </LinearLayout>

<ListView
    android:layout_width="wrap_content"
    android:layout_height="fillparent"
    android:id="@+id/theList"/>


</LinearLayout>

【讨论】:

  • 这个答案与我上面的答案没有什么不同。另外,您没有解释为什么使用margin 而不是padding 有效。在 SO 中不鼓励仅使用代码回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-16
  • 1970-01-01
  • 2017-07-11
  • 2015-04-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多