android - 如何为每个 Activity 的操作栏 Sherlock 创建自定义样式

标签 android styles actionbarsherlock

我在我的项目中使用 actionbarsherlock

并且我想根据我当前所处的 Activity 设置不同的操作栏样式

如何为每个 Activity 定义自定义操作栏样式?

最佳答案

在 oncreate 中试试这个,它会起作用

    setContentView(R.layout.mobile_change);
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setCustomView(R.layout.header);
    getSupportActionBar().setBackgroundDrawable(
            new ColorDrawable(Color.parseColor("#5B5292")));
    TextView txt = (TextView) findViewById(R.id.tv_title_header);
    Typeface font = Typeface.createFromAsset(getAssets(), "georgia.ttf");
    txt.setText("CHANGE MOBILE NO.");
    txt.setTextSize(13);
    txt.setTypeface(font);

如果您需要标题布局,请尝试使用此代码,否则您可以根据需要创建任何标题

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.4"
        android:gravity="center" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.2"
        android:gravity="center" >

        <TextView
            android:id="@+id/tv_title_header"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:gravity="center"
            android:singleLine="true"
            android:text=""
            android:textAllCaps="true"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/white"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.4"
        android:gravity="center"
        android:padding="2dp" >

        <ImageView
            android:id="@+id/iv_merchant_logo_header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:maxHeight="50dp" />
    </LinearLayout>
</LinearLayout>

关于android - 如何为每个 Activity 的操作栏 Sherlock 创建自定义样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18573566/

相关文章:

android - 操作栏图标叠加

android - 永远不会调用 Activity 的 onBackPressed 方法

java - 向特定用户发送解析推送通知

android - 迁移到 Material 设计 - 我们应该使用工具栏还是操作栏

javascript - 在 FF 上使用 native Javascript 进行样式属性操作

Android:如何设置AlertDialog的宽高和AlertDialog样式的按钮?

java - 如何为同步适配器设置 15 分钟的同步间隔。?

Android - 从服务器下载图像并加载到 ImageView

WPF DropShadowEffect 导致模糊

Android 3.0之前版本的action bar批量选择