繁体   English   中英

如何在协调器布局中添加工具栏标题?-Android

[英]how to add toolbar title in coordinator layout?- android

我有这个协调员的布局。 当前,当我折叠图像时,supportActionBar标题显示在顶部。 但是,当我折叠图像时,如何在工具栏中显示textView并在工具栏中显示菜单选项?

这是我的布局未折叠。 在此处输入图片说明

我希望工具栏的文本视图和optionsMenu的“编辑”按钮显示工具栏是否折叠。

这是当我折叠图像时。 在此处输入图片说明

因此,当图片合拢时,会显示我的supportActionBar文本。 当图像未折叠时,我不希望它出现在图像的前面。

这是我的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="RtlHardcoded"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:fitsSystemWindows="true">
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
            <!--ViewPager goes here-->
            <android.support.v4.view.ViewPager
                android:id="@+id/image_viewpager"
                android:layout_width="match_parent"
                android:layout_height="256dp"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                />
            <android.support.v7.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_collapseMode="parallax"
                android:id="@+id/card_info_polished_toolbar">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Card Info"
                    android:textColor="@color/white"
                    android:textSize="20dp"/>
            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <include layout="@layout/activity_5a_card_info_final_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

这是我的活动Java代码:在onCreate中,我有:

    myToolbar = (Toolbar) findViewById(R.id.card_info_polished_toolbar);
            setSupportActionBar(myToolbar);
            //getSupportActionBar().setDisplayShowTitleEnabled(false);
            //IF you use setSupportACtionBar, then you cannot use a textView for the title, the title is hidden. This is the only way to change it...
            Spannable text = new SpannableString("Sample Card info");
            text.setSpan(new ForegroundColorSpan(Color.WHITE), 0, text.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            getSupportActionBar().setTitle(text);
            getSupportActionBar().setHomeButtonEnabled(true);

然后对于工具栏上的编辑按钮,我有:

  public void editButtonClick(MenuItem item)
    {
        Intent i = new Intent(CardInfo_Polished.this, EditCardInfo.class);
        startActivity(i);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.menu_card_info,menu);
        if(Global_Class.getInstance().getValue().clickedCard.getCreatorName() == null || Global_Class.getInstance().getValue().user.getUsername() == null)
        {
            menu.getItem(0).setVisible(true);
        }
        //Code for later.
        /*if(Global_Class.getInstance().getValue().cardClicked.getUser_id().equals(Global_Class.getInstance().getValue().currentUserOUTDATED.getUUID()))
        {
            menu.getItem(0).setVisible(true);
        }
        else
        {
            menu.getItem(0).setVisible(false);
        }*/
        return true;
    }

像这样使用:

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


            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Toolbar Title"
                android:textColor="#ffffff"
                android:textSize="22dp"
                android:textStyle="bold" />

        </android.support.v7.widget.Toolbar>

我真的想通了。 您所要做的就是添加这一行代码。 工具栏内。

app:layout_collapseMode="pin"

我所拥有的是:

 app:layout_collapseMode="collapse"

这是一个示例...您可以使用此方法...希望您可以...否则您可以问我应用栏的布局仅添加了其余节点

<android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:fitsSystemWindows="true"
        android:layout_height="@dimen/app_bar_height_new"
        android:layout_width="match_parent"
        android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:fitsSystemWindows="true"
            android:layout_width="match_parent"

            android:layout_height="match_parent"
            app:layout_collapseMode="parallax"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleTextAppearance="@style/TransparentText"
            app:collapsedTitleTextAppearance="@style/WhiteText">
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <ImageView
                    android:id="@+id/img_main_image"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="centerCrop"
                    android:adjustViewBounds="true"
                    android:maxWidth="500dp"/>
                <ProgressBar
                    android:id="@+id/progressBar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:visibility="gone"
                    android:layout_centerInParent="true" />
                <com.zl.reik.dilatingdotsprogressbar.DilatingDotsProgressBar
                    android:id="@+id/progress"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:radius="5dp"
                    android:color="#ffffff"
                    app:dd_numDots="3"
                    app:dd_scaleMultiplier="1.5"
                    android:layout_centerInParent="true"
                    app:dd_animationDuration="500"
                    app:dd_horizontalSpacing="4dp"
                    />

            </RelativeLayout>
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_height="?attr/actionBarSize"
                android:layout_width="match_parent"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/Base.ThemeOverlay.AppCompat.Light"
                android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

活动中:

CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
collapsingToolbarLayout.setTitle("Enter the title Here");

请检查下面的代码

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/scale_300dp"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsingToolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax">

            <android.support.v4.view.ViewPager
                android:id="@+id/mPager"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <android.support.v7.widget.AppCompatImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/bottom_shadow" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:gravity="center_vertical|end"
                android:orientation="horizontal"
                android:padding="@dimen/scale_10dp">

                <!--<android.support.v7.widget.AppCompatTextView-->
                <!--android:id="@+id/tvBidPrice"-->
                <!--android:layout_width="0dp"-->
                <!--android:layout_height="wrap_content"-->
                <!--android:layout_weight="1"-->
                <!--android:text="Forging Scrap"-->
                <!--android:textColor="@color/white"-->
                <!--android:textSize="17sp"-->
                <!--android:textStyle="bold" />-->

                <android.support.v7.widget.AppCompatTextView
                    android:id="@+id/tvCountry"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="end"
                    android:drawableLeft="@drawable/india"
                    android:drawablePadding="@dimen/scale_10dp"
                    android:padding="@dimen/scale_5dp"
                    android:text="@string/CountryHint"
                    android:textColor="@color/white" />

            </LinearLayout>
        </FrameLayout>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:gravity="center"
            android:minHeight="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:theme="@style/AppTheme" />

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>
 </android.support.design.widget.CoordinatorLayout>


toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbar.setTitle("Your Title Here")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM