浮动操作按钮未正确显示
Floating Action Button doesn't appear correctly
我有一个floating action button
在我的Fragment
里面有一个TabLayout
。
页面加载时,按钮保持如下状态:
单击 "Quilometragem final" 字段后,键盘出现,然后我的按钮保持在正确位置:
如何解决此问题,以便在页面加载时将我的 floating action button
显示在正确的位置?
这是我的布局 xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:layout_marginLeft="@dimen/common_margin"
android:layout_marginRight="@dimen/common_margin">
<!-- another fields -->
<!-- KM FINAL -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TextView
android:text="@string/end_exam_mileage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="25sp"
android:fontFamily="sans-serif-condensed"
android:textColor="@color/primary" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/txtFinalKM"
android:fontFamily="sans-serif-condensed"
android:inputType="number"
android:hint="@string/end_exam_insert_mileage"
android:textSize="25sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
</LinearLayout>
<!-- NEXT BUTTON -->
<com.github.clans.fab.FloatingActionButton
android:id="@+id/fab_next_information"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginRight="@dimen/common_margin"
android:layout_marginBottom="@dimen/common_margin"
android:src="@drawable/icon_fab_mini_next"
android:clickable="true"
app:fab_shadowColor="#00FFFFFF"
app:fab_colorNormal="#272B35"
app:fab_colorPressed="@color/accent"
app:fab_colorRipple="#272B35" />
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
试试这个
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical">
<!-- another fields -->
<!-- KM FINAL -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed"
android:text="end_exam_mileage"
android:textSize="25sp"
android:textStyle="bold" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/txtFinalKM"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed"
android:hint="end_exam_insert_mileage"
android:inputType="number"
android:textSize="25sp" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
</LinearLayout>
<com.github.clans.fab.FloatingActionButton
android:id="@+id/fab_next_information"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:clickable="true"
android:src="@drawable/ic_message"
app:fab_colorNormal="#272B35"
app:fab_colorPressed="@color/accent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="20dp"
app:fab_colorRipple="#272B35"
app:fab_shadowColor="#00FFFFFF" />
</RelativeLayout>
将重力更改为底部|右侧
<com.github.clans.fab.FloatingActionButton
android:id="@+id/fab_next_information"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginRight="@dimen/common_margin"
android:layout_marginBottom="@dimen/common_margin"
android:src="@drawable/icon_fab_mini_next"
android:clickable="true"
app:fab_shadowColor="#00FFFFFF"
app:fab_colorNormal="#272B35"
app:fab_colorPressed="@color/accent"
app:fab_colorRipple="#272B35" />
您还可以在父布局中添加
android:clipChildren="false"
android:clipToPadding="false"
防止剪裁
感谢 Aswin P Ashok 的评论,我能够修复这个 bug 阅读 .
解决方案是在我的 Fragment
中的 OnCreateView
中添加以下代码:
_linearLayout.Post(new Runnable(() =>
{
_linearLayout.RequestLayout();
}));
为什么这有效?
如前所述here,
Posting requestLayout() works because the dependency graph is being built in the onMeasure() method
of CoordinatorLayout. After building the graph once, the
findAnchorView() method was called for every child. In the next pass
when building the graph in onMeasure() the method dependsOn() returns
correct result (unless some new views were added, for them it will not
work) and the graph builds correctly. It has to be called in post(),
because calling requestLayout() before the previous measure/layout
pass begins does not cause new pass.
我有一个floating action button
在我的Fragment
里面有一个TabLayout
。
页面加载时,按钮保持如下状态:
单击 "Quilometragem final" 字段后,键盘出现,然后我的按钮保持在正确位置:
如何解决此问题,以便在页面加载时将我的 floating action button
显示在正确的位置?
这是我的布局 xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:layout_marginLeft="@dimen/common_margin"
android:layout_marginRight="@dimen/common_margin">
<!-- another fields -->
<!-- KM FINAL -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TextView
android:text="@string/end_exam_mileage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="25sp"
android:fontFamily="sans-serif-condensed"
android:textColor="@color/primary" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/txtFinalKM"
android:fontFamily="sans-serif-condensed"
android:inputType="number"
android:hint="@string/end_exam_insert_mileage"
android:textSize="25sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
</LinearLayout>
<!-- NEXT BUTTON -->
<com.github.clans.fab.FloatingActionButton
android:id="@+id/fab_next_information"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginRight="@dimen/common_margin"
android:layout_marginBottom="@dimen/common_margin"
android:src="@drawable/icon_fab_mini_next"
android:clickable="true"
app:fab_shadowColor="#00FFFFFF"
app:fab_colorNormal="#272B35"
app:fab_colorPressed="@color/accent"
app:fab_colorRipple="#272B35" />
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
试试这个
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical">
<!-- another fields -->
<!-- KM FINAL -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed"
android:text="end_exam_mileage"
android:textSize="25sp"
android:textStyle="bold" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/txtFinalKM"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed"
android:hint="end_exam_insert_mileage"
android:inputType="number"
android:textSize="25sp" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
</LinearLayout>
<com.github.clans.fab.FloatingActionButton
android:id="@+id/fab_next_information"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:clickable="true"
android:src="@drawable/ic_message"
app:fab_colorNormal="#272B35"
app:fab_colorPressed="@color/accent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="20dp"
app:fab_colorRipple="#272B35"
app:fab_shadowColor="#00FFFFFF" />
</RelativeLayout>
将重力更改为底部|右侧
<com.github.clans.fab.FloatingActionButton
android:id="@+id/fab_next_information"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginRight="@dimen/common_margin"
android:layout_marginBottom="@dimen/common_margin"
android:src="@drawable/icon_fab_mini_next"
android:clickable="true"
app:fab_shadowColor="#00FFFFFF"
app:fab_colorNormal="#272B35"
app:fab_colorPressed="@color/accent"
app:fab_colorRipple="#272B35" />
您还可以在父布局中添加
android:clipChildren="false"
android:clipToPadding="false"
防止剪裁
感谢 Aswin P Ashok 的评论,我能够修复这个 bug 阅读
解决方案是在我的 Fragment
中的 OnCreateView
中添加以下代码:
_linearLayout.Post(new Runnable(() =>
{
_linearLayout.RequestLayout();
}));
为什么这有效?
如前所述here,
Posting requestLayout() works because the dependency graph is being built in the onMeasure() method of CoordinatorLayout. After building the graph once, the findAnchorView() method was called for every child. In the next pass when building the graph in onMeasure() the method dependsOn() returns correct result (unless some new views were added, for them it will not work) and the graph builds correctly. It has to be called in post(), because calling requestLayout() before the previous measure/layout pass begins does not cause new pass.