问题描述
我在启动屏幕活动上创建了三个滑动启动屏幕布局片段,我想更改每个片段的状态栏颜色以匹配片段主体的颜色。
我的代码是
Fragment_a.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FragmentA">
<ImageView
android:id="@+id/circle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/circle_filled"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"/>
<ImageView
android:id="@+id/circle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/circle_blank"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@id/circle1"
android:layout_marginTop="16dp"
android:layout_marginStart="8dp"/>
<ImageView
android:id="@+id/circle3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/circle_blank"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@id/circle2"
android:layout_marginTop="16dp"
android:layout_marginStart="8dp"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="32dp"
android:text="Skip"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_marginTop="120dp"
android:src="@drawable/ic_browsing"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="68dp"
android:text="Move"
android:textSize="20dp"
app:layout_constraintStart_toStartOf="@+id/imageView2"
app:layout_constraintTop_toBottomOf="@+id/imageView2" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Step by Step"
app:layout_constraintTop_toBottomOf="@id/textView2"
app:layout_constraintStart_toStartOf="@id/textView2"
android:layout_marginTop="6dp"
android:textSize="35sp"
android:textStyle="bold"
android:textColor="@color/black"/>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a dummy text.\nMade for testing purpose!"
android:textSize="20sp"
android:layout_marginTop="10dp"
app:layout_constraintStart_toStartOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/textView3" />
FragmentA.java
package in.pratikchakraborty.liquidswipe;
import android.os.Build;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
public class FragmentA extends Fragment {
// Todo: Rename parameter arguments,choose names that match
// the fragment initialization parameters,e.g. ARG_ITEM_NUMBER
private static final String ARG_ParaM1 = "param1";
private static final String ARG_ParaM2 = "param2";
// Todo: Rename and change types of parameters
private String mParam1;
private String mParam2;
public FragmentA() {
// required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment FragmentA.
*/
// Todo: Rename and change types and number of parameters
public static FragmentA newInstance(String param1,String param2) {
FragmentA fragment = new FragmentA();
Bundle args = new Bundle();
args.putString(ARG_ParaM1,param1);
args.putString(ARG_ParaM2,param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_ParaM1);
mParam2 = getArguments().getString(ARG_ParaM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_a,container,false);
if (Build.VERSION.SDK_INT >= 21) {
Window window = this.getActivity().getwindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYstem_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(this.getResources().getColor(R.color.white));
}
}
}
我在构建项目时遇到此错误。
AndroidStudioProjects\LiquidSwipe\app\src\main\java\in\pratikchakraborty\liquidswipe\FragmentA.java:65: error: unreachable statement
if (Build.VERSION.SDK_INT >= 21) {
^
解决方法
您正在 return 语句之后编写更改状态颜色的代码,因此无法访问,请尝试以下
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_a,container,false);
if (Build.VERSION.SDK_INT >= 21) {
Window window = this.getActivity().getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(this.getResources().getColor(R.color.white));
}
return view;
}