Java 在每个活动中使用样式化的操作栏
我想在操作栏中为我的应用程序名称使用自定义的颜色、字体和大小,所以我在MainActivity.java中对其进行了样式化Java 在每个活动中使用样式化的操作栏,java,android,android-actionbar,material-design,Java,Android,Android Actionbar,Material Design,我想在操作栏中为我的应用程序名称使用自定义的颜色、字体和大小,所以我在MainActivity.java中对其进行了样式化 //stylize the action bar TextView tv = new TextView(getApplicationContext()); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ActionBar.LayoutParams.WRAP_CONTE
//stylize the action bar
TextView tv = new TextView(getApplicationContext());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(lp);
tv.setText(R.string.Title);
tv.setTextSize(45);
tv.setTextColor(Color.parseColor("#FFFFFF"));
Typeface tf = Typeface.createFromAsset(getAssets(), "KGALittleSwag.ttf");
tv.setTypeface(tf);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(tv);
updateOptionsMenu();
这就成功了,然后当我添加另一个活动时,我将这段代码复制到其中,它看起来很棒。目前,我的应用程序几乎可以发布了,但我正在进行一些重构,现在我有6个左右的活动,将这些代码放在每个活动中感觉有点多余。是否有更好的做法可以将这些更改普遍应用于操作栏?这就是目的所在
创建一个抽象BaseActivity
类,您将在其中完成所有这些处理。要应用这些样式的所有活动都将继承BaseActivity
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
//stylize the action bar
TextView tv = new TextView(getApplicationContext());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams (ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(lp);
tv.setText(R.string.Title);
tv.setTextSize(45);
tv.setTextColor(Color.parseColor("#FFFFFF"));
Typeface tf = Typeface.createFromAsset(getAssets(), "KGALittleSwag.ttf");
tv.setTypeface(tf);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(tv);
updateOptionsMenu();
}
}
那么你的孩子们的活动:
public abstract class ChildActivity extends BaseActivity {
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState); // Here it calls the parent onCreate method and therefore executes the styling code
}
}
您所能做的只是为自定义操作栏创建一个
XML
布局文件,然后在所有活动中使用它
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/actionBarTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:gravity="center"
android:maxLines="1"
android:text="your text"
android:textAlignment="center"
android:textColor="#ffffff"
android:textSize="20dp"
android:textStyle="bold" />
</RelativeLayout>
这对我来说是个不错的选择,谢谢!但是有一个问题,我是在文件夹中创建BaseActivity类和所有其他类一起创建,还是有其他方法将其抽象化?@PaxanaNonGrata您可以在
activities
包的根目录上创建一个普通Java类(假设您有)。您可以像对待任何Java类一样对待它,唯一的区别是它是抽象的,没有XML布局。
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.custom_action_bar);