相对布局中的 TextView 和 ListView
TextView and ListView in Relative Layout
我正在尝试设置一个相对布局,在屏幕顶部有一个工具栏,在中心有一个列表视图。这是我的 xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textList"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/my_toolbar"
android:layout_centerHorizontal="true"
android:layout_marginTop="-48dp" />
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="match_parent" />
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
style="@style/HeaderBar"
android:elevation="4dp"/>
</RelativeLayout>
这是 Android Studio 中集成的编辑器中的样子:
我已经检查过,列表中只有一个元素,但是当我尝试打开它时,我在应用程序中看到的是:
会不会是TextView/ListView大小的问题?
编辑:
这是修改 xml 后的样子:
在您的布局中,ListView
使用所有 space,因为它没有像 android:layout_below="@id/textList"
.
这样的约束
你可以使用类似的东西:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
style="@style/HeaderBar"
android:elevation="4dp"/>
<TextView
android:id="@+id/textList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/my_toolbar"
android:layout_centerHorizontal="true"
android:text="test"/>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="@id/textList"/>
</RelativeLayout>
我正在尝试设置一个相对布局,在屏幕顶部有一个工具栏,在中心有一个列表视图。这是我的 xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textList"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/my_toolbar"
android:layout_centerHorizontal="true"
android:layout_marginTop="-48dp" />
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="match_parent" />
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
style="@style/HeaderBar"
android:elevation="4dp"/>
</RelativeLayout>
这是 Android Studio 中集成的编辑器中的样子:
我已经检查过,列表中只有一个元素,但是当我尝试打开它时,我在应用程序中看到的是:
会不会是TextView/ListView大小的问题?
编辑:
这是修改 xml 后的样子:
在您的布局中,ListView
使用所有 space,因为它没有像 android:layout_below="@id/textList"
.
你可以使用类似的东西:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
style="@style/HeaderBar"
android:elevation="4dp"/>
<TextView
android:id="@+id/textList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/my_toolbar"
android:layout_centerHorizontal="true"
android:text="test"/>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="@id/textList"/>
</RelativeLayout>