如何在工具栏中为每个片段设置标题和后退箭头按钮?

时间:2018-10-10 10:00:00

标签: java android

//这是我的片段

<RelativeLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white">

            <RelativeLayout
                android:id="@+id/background"
                android:layout_width="match_parent"
                android:layout_height="@dimen/_420sdp"
                android:background="#EAEAEA"
                >

                <RelativeLayout
                    android:id="@+id/home_bg_layout"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/_400sdp"
                    android:background="@drawable/sama">

                    <Button
                        android:id="@+id/bookAppointmentBtn"
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/_55sdp"
                        android:layout_marginLeft="@dimen/_30sdp"
                        android:layout_marginRight="@dimen/_30sdp"
                        android:fontFamily="@string/roboto_light"
                        android:textSize="@dimen/_20sdp"
                        android:layout_alignParentBottom="true"
                        android:layout_marginBottom="@dimen/_10sdp"
                        android:background="@drawable/book_appointmentbg"
                        android:textColor="@color/white"
                        android:text="@string/bookappointment" />

                </RelativeLayout>


            </RelativeLayout>

            <Button
                android:id="@+id/ContactNoBtn"
                android:layout_width="match_parent"
                android:layout_height="@dimen/_46sdp"
                android:text="@string/shop_number"
                android:textColor="#ffffff"
                android:textSize="@dimen/_25sdp"
                android:layout_below="@+id/discountsTv"
                android:textAllCaps="false"
                android:layout_marginRight="@dimen/_30sdp"
                android:layout_marginTop="@dimen/_5sdp"
                android:layout_marginLeft="@dimen/_30sdp"
                android:background="@drawable/black_background"/>


            <LinearLayout
                android:id="@+id/servicesLinearLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:baselineAligned="false"
                android:layout_marginLeft="@dimen/_20sdp"
                android:layout_marginRight="@dimen/_20sdp"
                android:orientation="horizontal"
                android:layout_below="@+id/servicesupperview"
                >

                <LinearLayout
                    android:id="@+id/menLayout"
                    android:layout_width="@dimen/_108sdp"
                    android:layout_height="@dimen/_27sdp"
                    android:layout_margin="@dimen/_10sdp"
                    android:layout_weight="1"
                    >
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@string/roboto_medium"
                        android:textColor="#535353"
                        android:layout_marginStart="@dimen/_17sdp"
                        android:layout_marginTop="@dimen/_5sdp"
                        android:textSize="@dimen/_12sdp"
                        android:text="@string/mens"
                        />

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/womenLayout"
                    android:layout_width="@dimen/_108sdp"
                    android:layout_height="@dimen/_27sdp"
                    android:layout_margin="@dimen/_10sdp"
                    android:layout_weight="1"
                    >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@string/roboto_medium"
                        android:textSize="@dimen/_12sdp"
                        android:layout_marginStart="@dimen/_10sdp"
                        android:layout_marginTop="@dimen/_5sdp"
                        android:textColor="#535353"
                        android:text="@string/womens"
                        />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/childrenLayout"
                    android:layout_width="@dimen/_108sdp"
                    android:layout_height="@dimen/_27sdp"
                    android:layout_margin="@dimen/_10sdp"
                    android:layout_weight="1">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="@dimen/_12sdp"
                        android:fontFamily="@string/roboto_medium"
                        android:textColor="#535353"
                        android:layout_marginStart="@dimen/_20sdp"
                        android:layout_marginTop="@dimen/_5sdp"
                        android:text="@string/children_caps"
                        />
                </LinearLayout>
            </LinearLayout>
    </ScrollView>
</RelativeLayout>

我在MainActivity中有工具栏。这是我要在工具栏内设置标题,图像和后退按钮的片段。在这里,我正在使用底部导航来更改片段。我想为每个片段设置不同的标题和图像。我必须将标题设置为工具栏的中心,并将后退按钮设置为工具栏的右上角。

3 个答案:

答案 0 :(得分:0)

在您的MainActivity中,将这些行添加到OnCreate()方法中

    if (getSupportActionBar() != null) {
        getSupportActionBar().setTitle("My Activity Title");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

要使用后退箭头,您可以在onCreate()之外编写此代码

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        Toast.makeText(this, "Back button pressed", Toast.LENGTH_SHORT).show();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

在片段中,您可以更改工具栏的标题

getActivity().setTitle("My fragment Title");

答案 1 :(得分:0)

您可以使用箭头ImageViewTextView创建自定义工具栏。 include_tool_bar.xml

<com.mastercard.wallet.ui.widget.CustomTextView
    android:id="@+id/toolbar_title"
    style="@style/Toolbar.TitleText"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical|left"
    android:textSize="@dimen/px_48"
    app:fontName="InterStateLight"
    customviews:fontName="InterStateLight" />

<ImageView
    android:id="@+id/toolbar_logo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:contentDescription="@string/adaCitiPayLogoText"
    android:src="@drawable/ab_logo"
    android:visibility="gone" />

并添加工具栏

Toolbar toolbar = (Toolbar) appCompatActivity.findViewById(R.id.toolbar);

  ImageView logo = (ImageView) toolbar.findViewById(R.id.toolbar_logo);
  CustomTextView textView = (CustomTextView) toolbar.findViewById(R.id.toolbar_title);

  logo.setVisibility(View.VISIBLE);
  logo.setImageResource(logoResourceId);
  textView.setVisibility(View.GONE);
  appCompatActivity.setSupportActionBar(toolbar);

 appCompatActivity.getSupportActionBar().setDisplayShowTitleEnabled(false);

答案 2 :(得分:0)

可以将工具栏

标题从片段更改为 onActivityCreated

     @Override
        public void onActivityCreated(@Nullable Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            ((MainActivity) getActivity()).getSupportActionBar().setTitle(getString(R.string.menu_about_us));
            ((MainActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            ((MainActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

在您的活动中,确保添加这样的工具栏。

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"/>

并将其设置为您的活动

setSupportActionBar(findViewById(R.id.toolbar));

对于后按,只需添加一个侦听器并执行需要的操作即可。

toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_action_back));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
       // pop up fragment or take another action

   }
});