parent
							
								
									4018b2c50d
								
							
						
					
					
						commit
						6b94113a49
					
				@ -0,0 +1,55 @@ | 
				
			|||||||
 | 
					package io.legado.app.base | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import android.os.Bundle | 
				
			||||||
 | 
					import android.view.MenuItem | 
				
			||||||
 | 
					import androidx.annotation.LayoutRes | 
				
			||||||
 | 
					import androidx.appcompat.app.AppCompatActivity | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					abstract class BaseActivity : AppCompatActivity() { | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @LayoutRes | 
				
			||||||
 | 
					    abstract fun getLayoutID(): Int | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    override fun onCreate(savedInstanceState: Bundle?) { | 
				
			||||||
 | 
					        super.onCreate(savedInstanceState) | 
				
			||||||
 | 
					        setContentView(getLayoutID()) | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    override fun onOptionsItemSelected(item: MenuItem?): Boolean { | 
				
			||||||
 | 
					        item?.let { | 
				
			||||||
 | 
					            if (it.itemId == android.R.id.home) { | 
				
			||||||
 | 
					                supportFinishAfterTransition() | 
				
			||||||
 | 
					                return true | 
				
			||||||
 | 
					            } | 
				
			||||||
 | 
					        } | 
				
			||||||
 | 
					        return if (item == null) true else onCompatOptionsItemSelected(item) | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    open fun onCompatOptionsItemSelected(item: MenuItem): Boolean { | 
				
			||||||
 | 
					        return true | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    override fun setTitle(title: CharSequence?) { | 
				
			||||||
 | 
					        supportActionBar?.let { | 
				
			||||||
 | 
					            it.title = title | 
				
			||||||
 | 
					        } | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    override fun setTitle(titleId: Int) { | 
				
			||||||
 | 
					        supportActionBar?.let { | 
				
			||||||
 | 
					            it.setTitle(titleId) | 
				
			||||||
 | 
					        } | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fun setSubTitle(subtitle: CharSequence?){ | 
				
			||||||
 | 
					        supportActionBar?.let { | 
				
			||||||
 | 
					            it.subtitle = subtitle; | 
				
			||||||
 | 
					        } | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fun setSubTitle(subtitleId: Int){ | 
				
			||||||
 | 
					        supportActionBar?.let { | 
				
			||||||
 | 
					            it.setSubtitle(subtitleId) | 
				
			||||||
 | 
					        } | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					} | 
				
			||||||
@ -0,0 +1 @@ | 
				
			|||||||
 | 
					## 基类 | 
				
			||||||
@ -0,0 +1,40 @@ | 
				
			|||||||
 | 
					package io.legado.app.ui.widget | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import android.content.Context | 
				
			||||||
 | 
					import android.util.AttributeSet | 
				
			||||||
 | 
					import android.widget.FrameLayout | 
				
			||||||
 | 
					import androidx.appcompat.app.AppCompatActivity | 
				
			||||||
 | 
					import io.legado.app.R | 
				
			||||||
 | 
					import kotlinx.android.synthetic.main.view_titlebar.view.* | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class TitleBar(context: Context, attrs: AttributeSet?) : FrameLayout(context, attrs) { | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    init { | 
				
			||||||
 | 
					        inflate(context, R.layout.view_titlebar, this) | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    override fun onAttachedToWindow() { | 
				
			||||||
 | 
					        super.onAttachedToWindow() | 
				
			||||||
 | 
					        attachToActivity() | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private fun attachToActivity(){ | 
				
			||||||
 | 
					        val activity = getCompatActivity(context) | 
				
			||||||
 | 
					        activity?.let { | 
				
			||||||
 | 
					            activity.setSupportActionBar(toolbar) | 
				
			||||||
 | 
					            activity.supportActionBar?.let { | 
				
			||||||
 | 
					                it.setDisplayHomeAsUpEnabled(true) | 
				
			||||||
 | 
					            } | 
				
			||||||
 | 
					        } | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private fun getCompatActivity(context: Context?): AppCompatActivity? { | 
				
			||||||
 | 
					        if (context == null) return null | 
				
			||||||
 | 
					        return when (context) { | 
				
			||||||
 | 
					            is AppCompatActivity -> context | 
				
			||||||
 | 
					            is androidx.appcompat.view.ContextThemeWrapper -> getCompatActivity(context.baseContext) | 
				
			||||||
 | 
					            is android.view.ContextThemeWrapper -> getCompatActivity(context.baseContext) | 
				
			||||||
 | 
					            else -> null | 
				
			||||||
 | 
					        } | 
				
			||||||
 | 
					    } | 
				
			||||||
 | 
					} | 
				
			||||||
@ -0,0 +1,16 @@ | 
				
			|||||||
 | 
					<?xml version="1.0" encoding="utf-8"?> | 
				
			||||||
 | 
					<com.google.android.material.appbar.AppBarLayout | 
				
			||||||
 | 
					        xmlns:android="http://schemas.android.com/apk/res/android" | 
				
			||||||
 | 
					        xmlns:app="http://schemas.android.com/apk/res-auto" | 
				
			||||||
 | 
					        android:id="@+id/appBar" | 
				
			||||||
 | 
					        android:layout_width="match_parent" | 
				
			||||||
 | 
					        android:layout_height="wrap_content" | 
				
			||||||
 | 
					        android:theme="@style/AppTheme.AppBarOverlay"> | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <androidx.appcompat.widget.Toolbar | 
				
			||||||
 | 
					            android:id="@+id/toolbar" | 
				
			||||||
 | 
					            android:layout_width="match_parent" | 
				
			||||||
 | 
					            android:layout_height="wrap_content" | 
				
			||||||
 | 
					            app:popupTheme="@style/AppTheme.PopupOverlay"/> | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					</com.google.android.material.appbar.AppBarLayout> | 
				
			||||||
					Loading…
					
					
				
		Reference in new issue