use*_*353 5 android android-layout android-xml
我的布局中有这个:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/primary"
android:paddingLeft="32dp"
android:paddingRight="32dp"
android:fitsSystemWindows="true">
...
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
但我的应用程序中没有paddingLeft或没有paddingRight.当我移除时fitsSystemWindows,填充物返回.为什么?我怎样才能保持fitsSystemWindows和填充?
fitsSyatemWindows属性会覆盖应用布局的填充。
因此,要应用填充,您应该为自己创建一个包装器布局,RelativeLayout并向其中添加fitsSystemWindows属性和paddingchild RelativeLayout。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/primary"
android:fitsSystemWindows="true"> //this is container layout
<RelativeLayout
android:paddingLeft="32dp"
android:paddingRight="32dp"
..... > //now you can add padding to this
.....
</RelativeLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
如果在使用fitSystemWindows时有人需要删除顶部填充,我将在此处添加它。使用自定义操作栏,DrawerLayout / NavigationView和/或片段时可能就是这种情况。
public class CustomFrameLayout extends FrameLayout {
public CustomFrameLayout(Context context) {
super(context);
}
public CustomFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CustomFrameLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected boolean fitSystemWindows(Rect insets) {
// this is added so we can "consume" the padding which is added because
// `android:fitsSystemWindows="true"` was added to the XML tag of View.
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN
&& Build.VERSION.SDK_INT < 20) {
insets.top = 0;
// remove height of NavBar so that it does add padding at bottom.
insets.bottom -= heightOfNavigationBar;
}
return super.fitSystemWindows(insets);
}
@Override
public WindowInsets onApplyWindowInsets(WindowInsets insets) {
// executed by API >= 20.
// removes the empty padding at the bottom which equals that of the height of NavBar.
setPadding(0, 0, 0, insets.getSystemWindowInsetBottom() - heightOfNavigationBar);
return insets.consumeSystemWindowInsets();
}
}
Run Code Online (Sandbox Code Playgroud)
我们必须扩展Layout类(在我的情况下为FrameLayout),并删除fitSystemWindows()(对于API <20)或onApplyWindowInsets()(对于API> = 20)中的顶部填充。
| 归档时间: |
|
| 查看次数: |
3073 次 |
| 最近记录: |