Android: 如何将 listView 对齐到自定义 actionBar 的底部?
Android: how to align listView to bottom of custom actionBar?
我正在按照 instructions here 将 actionBar
"float" 放在 activity 的内容之上,这样我的背景图像就会从屏幕的顶部边缘延伸出来在 actionBar
下。但是,现在我需要将 listView
对齐到 actionBar
的底部,因为 match_parent
现在将 listView
对齐到顶部边缘。
有没有办法只在 xml
中实现? actionBar
的 id
是什么,以便我可以设置 layout_below
?
我确定我可以在代码中 get/estimate actionBar
的高度来相应地设置 listView
的上边距,但我希望有一个更优雅的解决方案.谢谢
listView
activity xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_users"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="@drawable/img_people_512"
android:id="@+id/usersImageView"
android:alpha="0.55"
android:contentDescription="@string/backgroundImage"
android:scaleType="centerCrop" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/userListView"
/>
</RelativeLayout>
自定义 actionBar
布局 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"
android:id="@+id/mainActionBar"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/mainActionBarSpacer"
android:text="@string/mainActionBarSpacer"
android:textColor="@color/colourTransparent"
android:textSize="35sp"
android:fontFamily="cursive"
android:onClick="onClick" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/mainActionBarSpacer"
android:layout_toEndOf="@+id/mainActionBarSpacer"
android:gravity="center"
android:id="@+id/mainActionBarTitle"
android:text="@string/app_name"
android:textColor="@color/colourActionBarText"
android:textSize="35sp"
android:fontFamily="cursive"
android:onClick="onClick" />
</RelativeLayout>
只需将 layout_marginTop 添加到您的列表视图
android:layout_marginTop="?actionBarSize"
我正在按照 instructions here 将 actionBar
"float" 放在 activity 的内容之上,这样我的背景图像就会从屏幕的顶部边缘延伸出来在 actionBar
下。但是,现在我需要将 listView
对齐到 actionBar
的底部,因为 match_parent
现在将 listView
对齐到顶部边缘。
有没有办法只在 xml
中实现? actionBar
的 id
是什么,以便我可以设置 layout_below
?
我确定我可以在代码中 get/estimate actionBar
的高度来相应地设置 listView
的上边距,但我希望有一个更优雅的解决方案.谢谢
listView
activity xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_users"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="@drawable/img_people_512"
android:id="@+id/usersImageView"
android:alpha="0.55"
android:contentDescription="@string/backgroundImage"
android:scaleType="centerCrop" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/userListView"
/>
</RelativeLayout>
自定义 actionBar
布局 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"
android:id="@+id/mainActionBar"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/mainActionBarSpacer"
android:text="@string/mainActionBarSpacer"
android:textColor="@color/colourTransparent"
android:textSize="35sp"
android:fontFamily="cursive"
android:onClick="onClick" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/mainActionBarSpacer"
android:layout_toEndOf="@+id/mainActionBarSpacer"
android:gravity="center"
android:id="@+id/mainActionBarTitle"
android:text="@string/app_name"
android:textColor="@color/colourActionBarText"
android:textSize="35sp"
android:fontFamily="cursive"
android:onClick="onClick" />
</RelativeLayout>
只需将 layout_marginTop 添加到您的列表视图
android:layout_marginTop="?actionBarSize"