我想在摺疊工具欄中顯示一些文本而不是標題。問題是文本可能包含多於1行。所以我需要使用自定義視圖,但無法理解如何以適當的方式實現它。CollapsingToolbarLayout中的多行文本視圖而不是標題
另外,如何設置最小CollapsingToolbar
高度,總是顯示所有文本行,而不是將它們摺疊爲一個?
完全,我需要的是這樣的:
其中,1 - 啓動位和3 - 結束位置(達到此工具欄的高度之後就再也沒有崩潰)。
我想在摺疊工具欄中顯示一些文本而不是標題。問題是文本可能包含多於1行。所以我需要使用自定義視圖,但無法理解如何以適當的方式實現它。CollapsingToolbarLayout中的多行文本視圖而不是標題
另外,如何設置最小CollapsingToolbar
高度,總是顯示所有文本行,而不是將它們摺疊爲一個?
完全,我需要的是這樣的:
其中,1 - 啓動位和3 - 結束位置(達到此工具欄的高度之後就再也沒有崩潰)。
對於多行文本使用下面cade for Toolbar;
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/color"
app:popupTheme="@style/AppTheme.PopupOverlay">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#FFF"
android:inputType="textMultiLine"
/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
你可以嘗試使用這個很不錯的lib here。
自述文件解釋了關於如何在項目中添加和使用lib的一切。
如果您想在CollapsingToolbarLayout中進行自定義,那麼您需要了解相關信息。在android項目中添加CollapsingToolbarLayout很容易。如果您正在使用AndroidStudio,那麼很容易添加。
只需右鍵單擊您的包,如com.project
,選擇新建 - >活動 - >滾動活動和Add
它在您的項目中。現在你只需要用你的代碼做一些定製。
轉到你的風格文件,並添加這兩種風格: -
轉到您的activity_scrolling.xml文件,並設置兩個樣式在CollapsingToolbarLayout
。其他是在CollapsingToolbarLayout
中添加TextView
作爲您的要求。像這樣的: -
<?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"
android:fitsSystemWindows="true"
tools:context="com.mailcollection.ScrollingActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:collapsedTitleTextAppearance="@style/TextAppearance.MyApp.Title.Collapsed"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleTextAppearance="@style/TextAppearance.MyApp.Title.Expanded"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left|bottom"
android:orientation="vertical"
android:padding="10dp"
android:layout_marginBottom="20dp"
app:layout_collapseMode="parallax">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="@string/app_name"
android:textSize="15sp"/>
<TextView
android:id="@+id/tv_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textSize="20sp"
android:text="@string/title_description"/>
</LinearLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_scrolling"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
app:layout_anchor="@id/appBarLayout"
app:layout_anchorGravity="bottom|end"
app:srcCompat="@android:drawable/ic_dialog_email"/>
</android.support.design.widget.CoordinatorLayout>
當你做一些定製你的CollapsingToolbarLayout
的,你需要實現你的活動文件也有一些自定義代碼。
轉到您的ScrollingActivity.java文件: -
public class ScrollingActivity extends AppCompatActivity {
CollapsingToolbarLayout collapsingToolbar;
AppBarLayout appBarLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsingToolbar);
collapsingToolbar.setCollapsedTitleTextAppearance(R.style.TextAppearance_MyApp_Title_Collapsed);
collapsingToolbar.setExpandedTitleTextAppearance(R.style.TextAppearance_MyApp_Title_Expanded);
//This is the most important when you are putting custom textview in CollapsingToolbar
collapsingToolbar.setTitle(" ");
appBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
boolean isShow = false;
int scrollRange = -1;
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (scrollRange == -1) {
scrollRange = appBarLayout.getTotalScrollRange();
}
if (scrollRange + verticalOffset == 0) {
//when collapsingToolbar at that time display actionbar title
collapsingToolbar.setTitle(getResources().getString(R.string.app_name));
isShow = true;
} else if (isShow) {
//carefull there must a space between double quote otherwise it dose't work
collapsingToolbar.setTitle(" ");
isShow = false;
}
}
});
}
}
這是完整的代碼,如果你想添加多行TextView
在CollapsingToolbarLayout
,而不是標題。我希望你能得到你的解決方案。
當你實現這樣的代碼時,不需要設置最小的CollapsingToolbar高度,它總是根據文本長度顯示所有文本行。
看到這個liknk可能對你有幫助... https://stackoverflow.com/questions/34185843/android-collapsing-toolbar-how-to-resize-the-text-so-that-it-shows-全文-T – Vasant