在每个 activity 中使用风格化的操作栏

Use stylized action bar in every activity

我想在操作栏中为我的应用程序名称使用自定义颜色、字体和大小,所以我进入并在 MainActivity.java 中对其进行了风格化,就像这样

    //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();

这成功了,然后当我添加另一个 activity 时,我将这段代码复制到其中,看起来很棒。此时我的应用程序几乎可以发布了,但我正在进行一些重构,我现在有 6 个左右的活动,将这段代码放在每个活动中感觉有点多余。是否有更好的做法将这些更改普遍应用于操作栏?

这就是 inheritance 的用途。

创建一个 abstract BaseActivity Class,您将在其中进行所有这些处理。您要应用这些样式的所有活动都将继承 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();

    }
}

那么你的 children 活动:

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>

确保您的 TextView 居中,并且 TextViewlayout_widthlayout_hight 设置为 wrap_content

比将它放入每个 activity 您想要使用自定义操作栏

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.custom_action_bar);