【发布时间】:2019-06-11 21:28:20
【问题描述】:
我制作了一个应用程序,其中一个活动包含两个 TextView、一个 ImageView 和 FloatingActionButton。我想做这样的布局。
这是我的代码。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fillViewport="true"
tools:context=".DetailsActivity"
android:id="@+id/scrollView"
>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<!-- <ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" /> -->
<TextView
android:id="@+id/titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:textSize="25sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/blogContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:textSize="16sp"
/>
<ImageView
android:id="@+id/youtubeThumbnailImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="20dp"
android:src="@mipmap/ic_launcher"
android:visibility="gone"
/>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_margin="20dp"
android:src="@drawable/icons8_share_480"
app:fabSize="auto"
/>
</android.support.design.widget.CoordinatorLayout>
</ScrollView>
我使用 post 中的代码使其在所有 android api 上向下滚动时隐藏。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (scrollY > 0 && fab.isShown()) {
fab.setVisibility(View.GONE);
} else if (scrollY < 0) {
fab.setVisibility(View.VISIBLE);
}
}
});
} else {
scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
int mScrollY = scrollView.getScrollY();
if (mScrollY > 0 && fab.isShown()) {
fab.setVisibility(View.GONE);
} else if (mScrollY < 0) {
fab.setVisibility(View.VISIBLE);
}
}
});
}
这适用于 android 6.0。但高于 6.0,浮动按钮出现在活动的右上角。这是下面的图片。
我尝试了许多属性选项,例如-android:layout_gravity="",并将 FloatingButton 直接放在主 LinearLayout 或 FrameLayout 上。这些布局仍然不起作用。
【问题讨论】:
标签: java android android-layout android-widget floating-action-button