Java 根据片段更改工具栏
我想根据单击的Java 根据片段更改工具栏,java,android,xml,android-fragments,Java,Android,Xml,Android Fragments,我想根据单击的片段更改顶部的工具栏,但它似乎不适合我。 我想要的是,当我单击片段时,顶部工具栏应该会更改。例如,添加一个add按钮并更改Create Fragment工具栏的标题 Fragment.java import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; @Override public View onCreateView(LayoutInfla
片段更改顶部的工具栏
,但它似乎不适合我。
我想要的是,当我单击片段
时,顶部工具栏应该会更改。例如,添加一个add
按钮并更改Create Fragment
工具栏的标题
Fragment.java
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
setHasOptionsMenu(true);
View v = inflater.inflate(R.layout.fragment_event_created, container, false);
Toolbar toolbar123 = (Toolbar) v.findViewById(R.id.toolbar123);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar123);
//((MainActivity) getActivity()).getSupportActionBar().hide();
return v;
}
片段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="com.example.rick.check_in.ui.EventCreatedFragment">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar123"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#131313"
android:minHeight="?attr/actionBarSize">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#00aaaaaa">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000"
android:text="Delete"
android:textColor="#fff"
android:textSize="16dp" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
你可以这样做onAttach(Activity)
在片段与其活动关联后调用
public class YourFragment extends Fragment {
private MainActivity mainActivity;
private Toolbar toolbar123;
public YourFragment() {
// Required empty public constructor
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mainActivity = (MainActivity)activity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_event_created, container, false);
toolbar123 = (Toolbar)v.findViewById(R.id.toolbar123);
setupToolbar();
return v;
}
private void setupToolbar(){
toolbar123.setTitle(getString("Your Fragment Title"));
mainActivity.setSupportActionBar(toolbar123);
}
}
为什么使用工具栏内的按钮?我的意思是,这没有错,但是你可以使用带有图标的菜单项而不是按钮,片段可以有自己的菜单。如果您希望更改工具栏的颜色或类似功能,可以在“活动”中创建一个方法,并使用“活动实例”从片段中调用该方法。我看不出您在代码中尝试过这样做的任何地方。@Thracian您的意思是我只需为不同的片段设置菜单项图标(例如,设置标题,设置工具栏上菜单项的布局)?可以自定义不同的工具栏(不同的工具栏xml)对于不同的片段,并根据不同的片段设置不同工具栏的布局?我已经看到了Dinesh Neupane的答案,但这不是正确的方法。您不应该从片段访问活动视图。您应该使用一个或多个方法获取活动实例并设置视图属性是的,您可以更改标题和其他功能如果您在包含片段的活动中设置了该方法。您也可以更改菜单,在您的情况下,可以使用不同菜单的按钮来设置片段。您几乎看不到从片段访问的活动视图,这是可行的,但不推荐使用。保持事物分离、不耦合的方法是使用回调或Activity instanceToolBar应该是活动的视图,如果您希望在滑动手势上切换片段,则活动的ViewPager中应该有片段。不要将工具栏放在片段中。检查工具栏示例,您将看到构建正确材质设计布局的模式。