0

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

这是我的布局没有折叠。 enter image description here

我想要我的工具栏textview和我的optionsMenu“编辑”按钮来显示工具栏是否折叠。

而这是当我崩溃的形象。 enter image description here

所以我的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; 
    } 

回答

1

使用这样的:

<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> 
0

我其实已经想通了。你所要做的就是添加这一行代码。在工具栏内。

app:layout_collapseMode="pin" 

我有什么是这样的:

app:layout_collapseMode="collapse" 
0

这是一个例子... U可以计算出使用这个...希望如果u能......否则ü可以问我 布局应用栏布局的唯一其余节点加入

<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"); 
0

请检查下面的代码

<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") 
相关问题