【发布时间】:2019-08-11 22:56:50
【问题描述】:
我正在尝试在片段中添加 ActionBar(工具栏),然后将 Button 设置为该操作栏的“结束”并在该按钮上添加 onClickListener。
无法使用 support.v7.widget.ToolBar(我不知道为什么但我无法实现它)所以我不得不使用 androidx.appcompat.widget.Toolbar 代替。 我找不到任何来源。
app_bar_layout.xml:
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_app_toolbar"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</androidx.appcompat.widget.Toolbar>
Fragment的设计fragment_wall.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WallFragment"
android:background="@color/colorWhite">
<include
android:id="@+id/update_wall_page_toolbar"
layout="@layout/app_bar_layout"></include>
</FrameLayout>
WallFragment.java 中的代码
public class WallFragment extends Fragment {
public WallFragment() {
// Required empty public constructor
}
private Toolbar TopActivityToolbar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_wall, container, false);
TopActivityToolbar = v.findViewById(R.id.update_wall_page_toolbar);
TopActivityToolbar.setTitle("Príspevky");
TopActivityToolbar.setLogo(ContextCompat.getDrawable(getActivity(),R.drawable.ic_add_circle_black_24dp));
return v;
}
}
【问题讨论】:
-
这种行为有什么原因吗?由于片段驻留在活动中,因此在活动中添加工具栏更容易
-
那么到底是什么问题?
-
能否详细描述一下,在哪里(在什么fragment中)需要设置按钮,在其他fragment中是否需要App Bar?
标签: android android-fragments androidx