我有一个活动,我设置了一个工具栏。
MainActivity.class
@ContentView(R.layout.activity_main)
public class MainActivity extends RoboActionBarActivity {
@InjectView(R.id.tool_bar)
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setToolBar();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new SplashFragment())
.commit();
} else {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new HomeFragment())
.commit();
}
}
private void setToolBar() {
setSupportActionBar(toolbar);
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".activities.MainActivity">
<include
android:id="@+id/tool_bar"
layout="@layout/tool_bar" />
<View
android:id="@+id/shadow_view"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/tool_bar"
android:background="#ad8c22" />
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/shadow_view"
tools:context=".activities.MainActivity"
tools:ignore="MergeRootFrame" />
</RelativeLayout>
tool_bar.xml
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorToolBar"
>
<RelativeLayout
android:id="@+id/toolbar_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp">
<ImageView
android:id="@+id/tool_bar_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="toggleSlidingMenu"
android:src="@mipmap/icon_sliding_menu" />
<ImageView
android:id="@+id/app_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/jammboree" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/title"
android:visibility="gone"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/app_logo"
android:padding="5dp">
<ImageView
android:id="@+id/hotlist_bell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:contentDescription="bell"
android:gravity="center"
android:padding="7dp"
android:src="@mipmap/icon_my_cart" />
<TextView
android:id="@+id/hotlist_hot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/hotlist_bell"
android:layout_alignTop="@id/hotlist_bell"
android:background="@drawable/rounded_square"
android:gravity="center"
android:minWidth="17sp"
android:padding="2dp"
android:text="2"
android:textColor="#ffffffff"
android:textSize="12sp" />
<TextView
android:id="@+id/myMoneyInMyPocket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/hotlist_bell"
android:text="2000$"
android:textColor="#ffffff"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
当我去 HomeFragment.class 时一切正常。现在,当我从 HomeFragment.class 转移到 InfoFragment.class 时,我想更改我的工具栏。
InfoFragmnet.class
public class InfoFragment extends RoboFragment {
@InjectView(R.id.menuItemStores)
LinearLayout menuItemStores;
@InjectView(R.id.menuItemInsurance)
LinearLayout menuItemInsurance;
@InjectView(R.id.menuItemWishlist)
LinearLayout menuItemWishlist;
@InjectView(R.id.menuItemAccount)
LinearLayout menuItemAccount;
public InfoFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_info, container, false);
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setupUIElements();
}
}
这里我被阻止了,并没有找到如何更改工具栏并覆盖工具栏中项目的点击事件。
答案 0 :(得分:0)
如果您想更改工具栏的标题,只需致电toolbar.setTitle(xxx)
如果要更改选项菜单,请更新选项菜单资源并调用invalidateOptionsMenu();
希望这会对你有所帮助。