如何为所有活动设置相同的操作栏?

时间:2014-05-07 10:53:38

标签: android layout android-activity android-actionbar android-actionbar-compat

我试图为我的所有活动设置相同的操作栏(使用自定义字体),但是我的"方法"仅适用于名为LoginActivity的MainActivity。

public class LoginActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);


        this.getActionBar().setDisplayShowCustomEnabled(true);
        this.getActionBar().setDisplayShowTitleEnabled(false);

        LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.actionbar, null);
        Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Billabong.ttf");

        TextView title = ((TextView)v.findViewById(R.id.custom_bar));
        title.setText(this.getTitle());
        title.setTypeface(font);

        this.getActionBar().setCustomView(v);

如果我将同一段代码调用到另一个活动,例如MessagesActivity:

public class MessagesActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_messages);

        this.getActionBar().setDisplayShowCustomEnabled(true);
        this.getActionBar().setDisplayShowTitleEnabled(false);

        LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflator.inflate(R.layout.actionbar, null);
        Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Billabong.ttf");

        TextView title = ((TextView)v.findViewById(R.id.custom_bar));
        title.setText(this.getTitle());
        title.setTypeface(font);

        this.getActionBar().setCustomView(v);

当我点击按钮打开此活动时,我收到错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rram.wehere/com.rram.wehere.MessagesActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
        at android.app.ActivityThread.startActivityNow(ActivityThread.java:2045)
        at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
        at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
        at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
        at android.widget.TabHost.setCurrentTab(TabHost.java:413)
        at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)
        at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:548)
        at android.view.View.performClick(View.java:4438)
        at android.view.View$PerformClick.run(View.java:18439)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5149)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.rram.wehere.BaseActivity.onCreate(BaseActivity.java:23)
        at com.rram.wehere.MessagesActivity.onCreate(MessagesActivity.java:20)

这告诉我this.getActionBar()返回null,即使我已经声明主题有一个ActionBar,在我的Manifest中定义,稍后在我的styles.xml中定义

<resources>
    <style name="myTheme"  parent="@style/Theme.AppCompat.Light">
        <item name="android:actionBarStyle">@style/myTheme.ActionBar</item>
        <item name="android:actionMenuTextColor">@color/actionBarText</item>
    </style>

    <style name="myTheme.ActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">@color/bluefb</item>
        <item name="android:titleTextStyle">@style/myTheme.ActionBar.Text</item>
        <item name="android:gravity">center</item>
    </style>

    <style name="myTheme.ActionBar.Text" parent="@android:style/TextAppearance">
        <item name="android:textColor">@color/black</item>
        <item name="android:textStyle">bold</item>
    </style>

我不知道如何解决这个问题,有人可以帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:2)

根据需要设置一个带有Actionbar设置的Activity类,并将该活动扩展到整个应用程序中的其他活动。

答案 1 :(得分:0)

看到这个..

    public class ActionBarActivity extends Activity
    {


       setcontrol();

    }


    public static void setcontrol() {
        // TODO Auto-generated method stub
        ActionBar actionBar = getActionBar();
        LayoutInflater inflater = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.actionbar_custom_view_home, null);
        actionBar.setCustomView(view, new ActionBar.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setDisplayHomeAsUpEnabled(false);
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayUseLogoEnabled(false);

        customActionbar_back_btn = (Button) view.findViewById(R.id.customActionbar_back_btn);
        customActionbar_back_btn.setBackgroundDrawable(getResources().getDrawable(R.drawable.addnew_button));
        customActionbar_back_btn.setCompoundDrawablesWithIntrinsicBounds( R.drawable.abc_ic_ab_back_holo_dark, 0, 0, 0);
    }

现在在Activity中扩展此ActionBarActivity并使用此方法设置操作栏。

答案 2 :(得分:0)

大家好,我在所有活动中实现了相同的自定义操作栏/应用栏。

在这里,我从应用主题中隐藏默认应用栏并创建我自己的自定义应用栏并添加片段然后在所有活动中加载片段。

<androidx.cardview.widget.CardView 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="@dimen/dimen_60"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   app:cardElevation="@dimen/dimen_5"
   android:background="#ECF3F8">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginStart="@dimen/dimen_30">

        <ImageView
            android:layout_width="@dimen/dimen_25"
            android:layout_height="@dimen/dimen_25"
            android:id="@+id/iv_voice"
            android:src="@drawable/ic_voice"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:layout_width="@dimen/dimen_25"
            android:layout_height="@dimen/dimen_25"
            android:id="@+id/iv_battery"
            android:src="@drawable/ic_battery"
            app:layout_constraintStart_toEndOf="@id/iv_voice"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginStart="@dimen/dimen_20"/>

        <ImageView
            android:layout_width="@dimen/dimen_25"
            android:layout_height="@dimen/dimen_25"
            android:id="@+id/iv_wifi"
            android:src="@drawable/ic_wifi"
            app:layout_constraintStart_toEndOf="@id/iv_battery"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginStart="@dimen/dimen_20"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

    <TextClock
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tc_time"
        android:format12Hour="hh:mm a"
        android:textSize="@dimen/text_20"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginEnd="@dimen/dimen_30">

        <ImageView
            android:layout_width="@dimen/dimen_30"
            android:layout_height="@dimen/dimen_30"
            android:id="@+id/iv_more"
            android:src="@drawable/ic_more"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:layout_width="@dimen/dimen_25"
            android:layout_height="@dimen/dimen_25"
            android:id="@+id/iv_user"
            android:src="@drawable/ic_user"
            app:layout_constraintStart_toEndOf="@id/iv_more"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginStart="@dimen/dimen_20"/>

        <ImageView
            android:layout_width="@dimen/dimen_25"
            android:layout_height="@dimen/dimen_25"
            android:id="@+id/iv_map"
            android:src="@drawable/ic_map"
            app:layout_constraintStart_toEndOf="@id/iv_user"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginStart="@dimen/dimen_20"/>

        <ImageView
            android:layout_width="@dimen/dimen_25"
            android:layout_height="@dimen/dimen_25"
            android:id="@+id/iv_home"
            android:src="@drawable/ic_home"
            app:layout_constraintStart_toEndOf="@id/iv_map"
            app:layout_constraintTop_toTopOf="parent"
            android:layout_marginStart="@dimen/dimen_20"/>

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

这是我的应用栏 UI

然后创建片段并将上面的 UI 包含在 xml 文件中

 <include
    layout="@layout/app_bar_layout"
    android:id="@+id/app_bar_layout"/>

现在我们可以在片段中访问应用栏图标,我们可以添加事件监听器

public class AppBarFragment extends Fragment {

FragmentAppBarBinding binding;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    binding = FragmentAppBarBinding.inflate(getLayoutInflater());

    binding.appBarLayout.ivVoice.setOnClickListener(v ->
            startActivity(new Intent(requireActivity(), 
    ActivityContact.class)));


    return binding.getRoot();
}
}

现在我们需要在我们想要显示应用栏的所有活动中替换这个片段。

这是所有活动的java代码

 FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.replace(R.id.app_bar_layout, new AppBarFragment());
    transaction.commit();

这是所有活动的xml代码

<FrameLayout
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:id="@+id/app_bar_layout"
   app:layout_constraintTop_toTopOf="parent"/>

希望对你有帮助

-谢谢。