带有 ListView 的可滚动 ImageView
Scrollable ImageView with ListView
我有一个 ImageView,即封面照片。另一个 ImageView 即个人资料图片和一个 textview 即 Coverphoto 中的名称。这张封面照片下方有一个列表视图。我想让整个布局可滚动,即封面照片布局也应该使用 Listview 向上滚动。我试过 list.addHeaderView 但如何将封面照片与 list.addheaderview 中的个人资料照片和姓名合并?
将所有这些(封面照片、个人资料照片、姓名等)放在一个布局中(例如 LinearLayout
| RelativeLayout
),然后将它们膨胀并添加到您的 Listview
.
您的代码可能是这样的:
View profielView = LayoutInflater.from(getActivity()).inflate(R.layout.view_your_profile, mListView, false);
mListView.addHeaderView(profileView);
你的view_your_profile
是这样的:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_profile_header_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- cover photo -->
<ImageView
android:id="@+id/image_profile_cover"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_launcher"
android:scaleType="centerCrop" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<!-- username -->
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="8dp"
android:text="@string/icon_chevron_left"
android:textColor="@color/white_transparent_30" />
<!-- profile photo -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"/>
</LinearLayout>
</RelativeLayout>
还有更好的方法使用 RecyclerView
和不同的视图类型,您可以找到很好的教程 here。
您需要为您的封面照片制作一个 RelativeLayout 并将个人资料图片和文本视图放入其中,例如在名为 header.xml 的文件中。
你可以参考这些问题:
Android, ImageView over ImageView
Adding text to ImageView in Android
因此,在创建 header.xml 之后,您可以将其与封面照片、个人资料图片、文本一起添加到您的 ListView 中,它会随着您滚动列表视图而滚动!
我有一个 ImageView,即封面照片。另一个 ImageView 即个人资料图片和一个 textview 即 Coverphoto 中的名称。这张封面照片下方有一个列表视图。我想让整个布局可滚动,即封面照片布局也应该使用 Listview 向上滚动。我试过 list.addHeaderView 但如何将封面照片与 list.addheaderview 中的个人资料照片和姓名合并?
将所有这些(封面照片、个人资料照片、姓名等)放在一个布局中(例如 LinearLayout
| RelativeLayout
),然后将它们膨胀并添加到您的 Listview
.
您的代码可能是这样的:
View profielView = LayoutInflater.from(getActivity()).inflate(R.layout.view_your_profile, mListView, false);
mListView.addHeaderView(profileView);
你的view_your_profile
是这样的:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_profile_header_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- cover photo -->
<ImageView
android:id="@+id/image_profile_cover"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_launcher"
android:scaleType="centerCrop" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<!-- username -->
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="8dp"
android:text="@string/icon_chevron_left"
android:textColor="@color/white_transparent_30" />
<!-- profile photo -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"/>
</LinearLayout>
</RelativeLayout>
还有更好的方法使用 RecyclerView
和不同的视图类型,您可以找到很好的教程 here。
您需要为您的封面照片制作一个 RelativeLayout 并将个人资料图片和文本视图放入其中,例如在名为 header.xml 的文件中。
你可以参考这些问题:
Android, ImageView over ImageView
Adding text to ImageView in Android
因此,在创建 header.xml 之后,您可以将其与封面照片、个人资料图片、文本一起添加到您的 ListView 中,它会随着您滚动列表视图而滚动!