Skip to content

Commit

Permalink
适配fragment换肤
Browse files Browse the repository at this point in the history
  • Loading branch information
mxlei committed Aug 31, 2022
1 parent 9aaf17c commit 5edd1a1
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 16 deletions.
10 changes: 5 additions & 5 deletions app/src/main/java/com/github/jeffery/skin/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.github.jeffery.skin;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import java.io.File;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

Expand All @@ -35,6 +34,7 @@ protected void onCreate(Bundle savedInstanceState) {
SkinManger.getInstance().setAppSkin(file12.getAbsolutePath());
tvSkinName.setText(getCurrentSkinName());
});
findViewById(R.id.btn_fragment).setOnClickListener(v -> startActivity(new Intent(v.getContext(), SecondActivity.class)));
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/java/com/github/jeffery/skin/TestFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.jeffery.skin;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

/**
* @author mxlei
* @date 2022/8/31
*/
public class TestFragment extends Fragment {

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_test, container, false);
}
}
9 changes: 8 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down Expand Up @@ -45,6 +44,14 @@
android:text="高贵红"
android:textColor="@color/buttonTextColor" />

<Button
android:id="@+id/btn_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="@drawable/bg_button"
android:text="Fragment测试"
android:textColor="@color/buttonTextColor" />
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
Expand Down
10 changes: 7 additions & 3 deletions app/src/main/res/layout/activity_second.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?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"
<FrameLayout 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"
tools:context=".SecondActivity">

</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.fragment.app.FragmentContainerView
android:id="@+id/container"
android:name="com.github.jeffery.skin.TestFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
41 changes: 41 additions & 0 deletions app/src/main/res/layout/fragment_test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">


<Button
android:id="@+id/btn_default_skin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@drawable/bg_button"
android:text="默认皮肤"
android:textColor="@color/buttonTextColor" />

<Button
android:id="@+id/btn_gray_skin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="@drawable/bg_button"
android:text="儒雅灰"
android:textColor="@color/buttonTextColor" />

<Button
android:id="@+id/btn_red_skin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="@drawable/bg_button"
android:text="高贵红"
android:textColor="@color/buttonTextColor" />

<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="16dp"
android:background="@drawable/avatar" />
</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,6 @@ public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle saved
activityStack.push(skinActivityRecord);
}

@Override
public void onActivityPostCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
super.onActivityPostCreated(activity, savedInstanceState);
// 应用皮肤到本界面
reloadSkin(getActivityRecord(activity));
}

@Override
public void onActivityResumed(@NonNull Activity activity) {
super.onActivityResumed(activity);
Expand Down

0 comments on commit 5edd1a1

Please sign in to comment.