【问题标题】:Passing calling activity to pager adapter将调用活动传递给寻呼机适配器
【发布时间】:2018-08-20 17:29:37
【问题描述】:

尝试将调用活动发送到寻呼机适配器,以便这两行在寻呼机适配器中工作:

((AppCompatActivity) callingActivity).setSupportActionBar(toolbar);

((AppCompatActivity)callingActivity).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

(注意使用寻呼机适配器而不是分段寻呼机适配器)。

【问题讨论】:

  • 通过构造函数?还有什么?
  • 是的构造函数,android studio 不会让调用活动成为构造函数调用的一部分
  • 嗯?您到底要做什么,目前还不清楚,请与代码一起具体说明问题
  • @Pavneet_Singh,您看到评论上方的编辑了吗?我正在尝试在工具栏中启用“后退按钮”和自定义标题。
  • @Emuna613 你能贴出片段构造函数的代码吗?

标签: android android-pageradapter


【解决方案1】:

您可以使用以下代码来做到这一点:

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light"
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

<TextView
    android:id="@+id/toolbar_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/app_name"
    android:textColor="@color/white"
    android:textSize="@dimen/text_size_medium" />

</android.support.v7.widget.Toolbar>

并将此代码放入您的活动中

Typeface typeface = Typeface.createFromAsset(getAssets(), SCTypeface.TTF_REGULAR);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
        }

        TextView toolbarTitle = (TextView) findViewById(R.id.toolbar_text);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        toolbarTitle.setText("YOUR TITLE");
        toolbarTitle.setTypeface(typeface);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();

            }
        });

【讨论】:

  • 我知道,我试图将活动(在构造函数中)传递给寻呼机适配器,因为我在那里需要它并且试图用 MyActivity.class 而不是 MyActivity.this 来做到这一点,大声笑
  • @Emuna613 表示您想从寻呼机适配器设置标题??
  • 是的,我是。从那以后我已经改变了实现,无论如何感谢@Garg's
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多