在我的应用中,我们必须使用自定义app-bar / ToolBar。 现在我们必须实施徽章(添加带计数的购物车)。
下面的是我的工具栏xml(toolbar.xml)
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue_074A92"
android:elevation="@dimen/appbar_elevation"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="100"
android:gravity="center_horizontal"
android:id="@+id/default_action_bar"
android:visibility="visible">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="30"
android:clickable="true"
android:gravity="left">
<ImageView
style="@style/app_bar"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:id="@+id/tv_language"
android:textColor="@color/white"
android:textSize="@dimen/app_bar_ln_text_size"
android:clickable="true"
android:src="@drawable/worlwide"
android:layout_marginLeft="@dimen/app_bar_tbv_margin_left_right"/>
<TextView
style="@style/app_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_language_txt"
android:textColor="@color/white"
android:textSize="@dimen/app_bar_ln_txt_text_size"
android:layout_gravity="center_vertical"
android:clickable="true"
android:text="@string/ln_english_txt"
android:layout_marginLeft="@dimen/activity_vertical_margin_ot"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="40"
android:gravity="center">
<TextView
style="@style/appbar_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_toolbar_title"
android:textColor="@color/white"
android:singleLine="true"
android:ellipsize="end"
android:text="Title"
android:textSize="@dimen/app_bar_text_size" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="30"
android:clickable="true"
android:gravity="right">
<ImageView
android:id="@+id/tv_search"
style="@style/app_bar"
android:layout_width="30dp"
android:layout_marginRight="@dimen/app_bar_tbv_margin_left_right"
android:duplicateParentState="true"
android:text="@string/icon_search"
android:src="@drawable/shopping_cart"
android:layout_height="match_parent" />
<ImageView
android:id="@+id/tv_logout"
style="@style/app_bar"
android:layout_width="30dp"
android:layout_marginRight="@dimen/app_bar_tbv_margin_left_right"
android:duplicateParentState="true"
android:text="@string/icon_logout"
android:src="@drawable/magnifying_glass"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="100"
android:gravity="center_horizontal"
android:id="@+id/search_action_bar"
android:visibility="gone">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="85"
android:clickable="true"
android:gravity="left"
android:orientation="horizontal"
android:background="@drawable/search_bottom_border"
android:layout_marginLeft="10dp"
android:padding="5dp">
<AutoCompleteTextView
style="@style/app_bar"
android:layout_width="0dp"
android:layout_weight="90"
android:layout_height="match_parent"
android:id="@+id/et_search"
android:textColor="@color/white"
android:textSize="16sp"
android:gravity="left"
android:imeOptions="actionSearch"
android:hint="@string/search_txt"
android:textColorHint="@color/grey_ecebf1"
android:singleLine="true"
android:background="@drawable/empty_bottom_border"/>
<TextView
android:layout_width="0dp"
android:layout_weight="10"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:text="@string/icon_cancel"
android:id="@+id/cancel_search_bar"
android:textColor="@color/white"
android:clickable="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="15"
android:clickable="true"
android:gravity="right">
<TextView
android:id="@+id/tv_logout_1"
style="@style/app_bar"
android:layout_marginRight="@dimen/app_bar_tbv_margin_left_right"
android:duplicateParentState="true"
android:text="@string/icon_logout"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.Toolbar>
我当前的片段代码,用于访问firstfragment.java文件中的app-bar
private void setUpTopBar(){
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
if (toolbar != null) {
LinearLayout default_action_bar = (LinearLayout)toolbar.findViewById(R.id.default_action_bar);
default_action_bar.setVisibility(View.VISIBLE);
TextView tv_activity_title = (TextView) toolbar.findViewById(R.id.tv_toolbar_title);
tv_activity_title.setText(getResources().getText(R.string.title_activity_home));
ImageView tv_language = (ImageView) toolbar.findViewById(R.id.tv_language);
tv_language.setVisibility(View.VISIBLE);
tv_language.setOnClickListener(this);
tv_language_txt = (ImageView) toolbar.findViewById(R.id.tv_language_txt);
tv_language_txt.setVisibility(View.VISIBLE);
tv_language_txt.setOnClickListener(this);
ImageView tv_search = (ImageView) toolbar.findViewById(R.id.tv_search);
tv_search.setVisibility(View.VISIBLE);
}
}
我目前的观点如下:
实际上我想要购物车图像上的计数器看起来像
请建议