1.根布局
import android.annotation.TargetApi; import android.content.Context; import android.graphics.Rect; import android.os.Build; import android.util.AttributeSet; import android.view.WindowInsets; import android.widget.RelativeLayout; /** * Created by Administrator on 2018/2/28. */ public class SoftInputRelativeLayout extends RelativeLayout { public SoftInputRelativeLayout(Context context) { super(context); } public SoftInputRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public SoftInputRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } public SoftInputRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected boolean fitSystemWindows(Rect insets) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { insets.left = 0; insets.top = 0; insets.right = 0; } return super.fitSystemWindows(insets); } @TargetApi(Build.VERSION_CODES.KITKAT_WATCH) @Override public WindowInsets onApplyWindowInsets(WindowInsets insets) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { return super.onApplyWindowInsets(insets.replaceSystemWindowInsets(0, 0, 0, insets.getSystemWindowInsetBottom())); } else { return insets; } } }
2.根布局添加
android:fitsSystemWindows="true"
3.代码
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //透明状态栏 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明导航栏 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); View v = ll.getChildAt(0); v.setPadding(0, getStatusBarHeight(), 0, 0); }参考自:https://www.jianshu.com/p/1b22a1d2a7b8