1

I'm trying to write a BaseToolBarActivity in an library module. I want to use light theme in app module ,but a dark theme on toolbar then I can get a light text color and so on. But it dosen't work when use BaseToolBarActivity in library module. Code like this

  public abstract class BaseToolBarActivity extends BaseActivity {
    private Toolbar mToolBar;
    private TextView mTextView;
    private RelativeLayout rootView;
    @Override
    protected final void initView() {
        setContentView(R.layout.activity_base_title);
        rootView = (RelativeLayout) findViewById(R.id.rl_rootView);
        mToolBar = (Toolbar)findViewById(R.id.tb_baseToolbar);
     mToolBar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
        mTextView = (TextView) findViewById(R.id.tv_baseTitle);
        mTextView.setText(getTitleBarText());
        View userView = LayoutInflater.from(this).inflate(getLayoutResource(), null);
        if (userView != null) {
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            layoutParams.addRule(RelativeLayout.BELOW, R.id.tb_baseToolbar);
            rootView.addView(userView, layoutParams);
            mUnBinder = ButterKnife.bind(this, userView);
        }
              setSupportActionBar(mToolBar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        StatusBarCompat.compat(this);
    }
    protected abstract String getTitleBarText();
    protected void enableBackIcon(boolean show) {
        if (show) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        } else {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            mToolBar.setNavigationOnClickListener(null);
        }
    }
    protected void setTitleBarColor(@ColorInt int titleBarColor) {
        this.setTitleBarColor(titleBarColor, -1);
    }
 protected void setTitleBarText(String title) {
        mTextView.setText(title);
    }
    protected void setTitleBarColor(@ColorInt int titleBarColor, @ColorInt int statusBarColor) {
        mToolBar.setBackgroundColor(titleBarColor);
        StatusBarCompat.compat(this, statusBarColor);
    }
    protected void setTitleSize(float size) {
        mTextView.setTextSize(size);
    }
    protected Toolbar getToolbarInstance() {
        return mToolBar;
    }
    protected void setTitleTextColor(@ColorInt int titleBarColor) {
        mTextView.setTextColor(titleBarColor);
    }
}

I configed the style in xml

 <android.support.v7.widget.Toolbar
        android:id="@+id/tb_baseToolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/AppTheme.Toolbar">
        <TextView
            android:id="@+id/tv_baseTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textColor="#ffffff"
            android:textSize="18sp" />
    </android.support.v7.widget.Toolbar>


<style name="AppTheme.Toolbar" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">?actionBarSize</item>
        <item name="android:textColorPrimaryInverse">@color/white</item>
        <item name="android:textColorPrimary">@color/white</item>
        <item name="android:actionMenuTextColor">@android:color/holo_green_light</item>
    </style>

but this not work!!!

I still try the same code in an app module,and it works,how could I config it in an libray.

Leo
  • 13
  • 4
  • Does your library have the same package name as your app? Probably not... You might need to reference `@style/AppTheme.Toolbar` using the package name of the library. – OneCricketeer Aug 17 '17 at 03:28
  • why I should do that? And I will use this library in all my app,it is impossible to use the same package name – Leo Aug 17 '17 at 03:52
  • I've not used styles from a custom library, but I just know that using `@style` would use the current application package name. See the documentation https://developer.android.com/guide/topics/resources/accessing-resources.html and this post. https://stackoverflow.com/questions/16053141/how-to-access-resources-in-a-android-library-project – OneCricketeer Aug 17 '17 at 12:33
  • Did you figure out a solution for this? I need to be able to theme a library module that is used by another project. – dazza5000 Mar 07 '18 at 00:44

0 Answers0