【问题标题】:Change ActionBar title using Fragments使用 Fragments 更改 ActionBar 标题
【发布时间】:2015-02-08 02:14:12
【问题描述】:
  • 我正在使用操作栏。
  • 我有一个片段通话收藏夹,我将一些联系人保存为收藏夹。
  • 当您单击一个联系人时,它会将我带到另一个片段,并将联系人号码放在 editText 上。
  • 新片段是调用 Transfers。

所以我的问题是,当用户单击联系人时,将我带到另一个片段,但操作栏上的标题仍然是最喜欢的标题而不是新标题,如何更改该标题?

我已经尝试在点击方法上使用setTitle,但仍然无法正常工作。

【问题讨论】:

标签: android android-fragments android-actionbar


【解决方案1】:

在你的活动中:

public void setActionBarTitle(String title) {
    getSupportActionBar().setTitle(title);
}

在你的片段中(你可以把它放在 onCreate 或 onResume):

 public void onResume(){
        super.onResume();

    // Set title bar
    ((MainFragmentActivity) getActivity())
            .setActionBarTitle("Your title");

}

【讨论】:

  • 很棒的示例,我没有意识到 onResume 可以覆盖我的标题
  • 它完全改变了,但它在改变后保存到所有片段
  • 如果我要更改片段,它应该被更改,因为我在 BottomNavigationViewEx 上有片段,它不适用于更改片段
【解决方案2】:

在你的片段中

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    getActivity().setTitle("Team B");

    View rootView = inflater.inflate(R.layout.fragment_team_b, container, false);

    return rootView;
}

【讨论】:

  • 谢谢!!这对我有用。
【解决方案3】:

如果您使用的是 Android Jetpack 中的导航组件。操作栏读取片段名称的标签属性。不确定这是否是一个正确的修复,但如果您在导航编辑器中更改标签文本,它将由托管片段的 Activity 中设置的 supportActionBar 读取。

【讨论】:

  • 我们可以使用jetpack改变action bar的字体吗?
【解决方案4】:

Em Kotlin você pode usar o seguinte código nas funções: onCreateView, onStart e onResume。

override fun onStart() {
   super.onStart()

   (activity as? AppCompatActivity)?.supportActionBar?.title = "Título".
}

Ou se preferir, pode criar uma função de extensão e chamar dentro do Fragment。

import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment

fun Fragment.setTitle(title: String) {
    (activity as? AppCompatActivity)?.supportActionBar?.title = title
}

Chamada da função de extensão dentro do Fragment。

class StartFragment : Fragment() {

   override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
   ): View? {
       return inflater.inflate(R.layout.fragment_start, container, false)
   }

   override fun onStart() {
       super.onStart()

       this.setTitle(getString(R.string.titulo))
   }
}

【讨论】:

    【解决方案5】:

    NOT_A_PROGRAMMER 的答案是正确的,但是当您返回上一个活动或片段时,标题保持不变。

    这是我的解决方案。

    • 在片段中覆盖 OnPause() 或 OnStopMethod,您可以使用相同的方法“((MainFragmentActivity) getActivity()).setActionBarTitle("Your title");”来设置存在的 Activity 或 Fragment 的标题在后台。
        @Override
        public void onStop() {
            super.onStop();
            ((MPOSTransactionActivity) getActivity()).setActionBarTitle(getString(//NAME));
        }
    

    【讨论】:

      【解决方案6】:

      供java使用
      更改工具栏的标题

      @Override
      public View onCreateView(LayoutInflater inflater,
                               ViewGroup container, Bundle savedInstanceState) {
      
          ((MainActivity) getActivity()).getSupportActionBar().setTitle("hello word");
      
          View root = inflater.inflate(R.layout.fragment_home, container, false);
          return root;
      }
      
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多