Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在屏幕顶部下方启动ListView_Java_Android_Xml_Listview - Fatal编程技术网

Java 在屏幕顶部下方启动ListView

Java 在屏幕顶部下方启动ListView,java,android,xml,listview,Java,Android,Xml,Listview,我正在尝试在我的列表视图上方添加某种填充或边距,以便添加带有图像和按钮的自定义导航栏。我已经尝试在XML中添加了填充和边距,但我的ListView似乎仍然从顶部开始 如何启动列表视图,使其低于屏幕顶部大约50p 布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_w

我正在尝试在我的
列表视图
上方添加某种填充或边距,以便添加带有图像和按钮的自定义导航栏。我已经尝试在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>

填充
在应用它的容器的边界内添加空间。您可以在
列表视图上使用
marginTop
margin
在应用它的容器的边界之外添加空间),也可以在
相对视图上使用
paddingTop

对于您的情况,您可能应该使用,并将其放置在
列表视图的上方。例如,如果使用
线性布局
而不是
相对布局
,只需给出
工具栏
维度,就会将
列表视图推到其下方

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

类似于此的内容,请尝试为
ListView

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


请添加您的布局xml添加您所拥有和想要的图片。我似乎找不到工具栏。它应该在widgets下吗?
工具栏
是在API 21中添加的。您可以在以前的版本中使用它的SupportV7版本。您需要将
compile'com.android.support:appcompat-v7:21.0.3'
添加到模块的
build.gradle
我目前有这个
compile'com.android.support:appcompat-v7:21.0.3'
请记住,当视图未包含在XML层次结构中时,将其添加到XML层次结构时,需要完全指定视图的整个包名SDK本身。在这种情况下,
这个答案与我上面的答案没有任何不同。另外,您没有解释为什么使用
margin
而不是
padding
有效。在SO中,不鼓励只使用代码的答案。