Java 带浮动按钮的滚动视图
我制作了一个应用程序,其中一个活动包含两个TextView、一个ImageView和FloatingActionButton。我想做这样的布局 这是我的密码Java 带浮动按钮的滚动视图,java,android,android-layout,android-widget,floating-action-button,Java,Android,Android Layout,Android Widget,Floating Action Button,我制作了一个应用程序,其中一个活动包含两个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"
<?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>
使其在所有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);
}
}
});
}
if(Build.VERSION.SDK\u INT>=Build.VERSION\u CODES.M){
scrollView.setOnScrollChangeListener(新视图.OnScrollChangeListener(){
@凌驾
public void onScrollChange(视图v、int-scrollX、int-scrollY、int-oldScrollX、int-oldScrollY){
如果(滚动>0&&fab.isShown()){
工厂设置可见性(视图已消失);
}else if(滚动<0){
工厂设置可见性(视图可见);
}
}
});
}否则{
scrollView.getViewTreeObserver().addOnScrollChangedListener(新的ViewTreeObserver.OnScrollChangedListener(){
@凌驾
public void onScrollChanged(){
int mScrollY=scrollView.getScrollY();
如果(mScrollY>0&&fab.isShown()){
工厂设置可见性(视图已消失);
}else if(mScrollY<0){
工厂设置可见性(视图可见);
}
}
});
}
这适用于android 6.0。但高于6.0时,浮动按钮出现在活动的右上角。这是下面的图片
我尝试了许多属性选项,比如-android:layout\u gravity=“
,并将FloatingButton直接放在主线布局或FrameLayout上。这些布局仍然不起作用。使用坐标布局,您应该尝试设置浮动操作按钮锚定以参考线性布局,并使用布局锚定重力来定位晶圆厂
工厂的示例代码:
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="@id/linearLayoutID"
app:layout_anchorGravity="bottom|right|end"
android:layout_margin="16dp"
android:src="@drawable/icons8_share_480"
/>
记住为您的LinearLayout设置一个ID。我使用了@id/linearLayoutID
作为占位符。我看到Android资源链接失败输出的错误:D:\Courses\Java\Android Projects\DummyApp\app\src\main\res\layout\activity\u details.xml:16:错误:属性Android:layout\u未找到锚。D:\Courses\Java\Android Projects\DummyApp\app\src\main\res\layout\activity\u details.xml:16:错误:未找到属性Android:layout\u ancorgravity。感谢您的编辑,一定错过了。我希望这个解决方案对你有效。