CollapsingToolbarLayout中的标题和副标题

时间:2015-08-22 22:23:06

标签: android material-design

我正在尝试创建一个像WhatsApp一样的个人资料活动,有3个文本视图而不是像WhatsApp一样,前两个应该在工具栏中折叠时崩溃,最后一个应该淡出。但我无法弄清楚如何,我已经尝试过这么多,这是我的代码

XML文件:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:background="@drawable/img"
    android:theme="@style/ThemeOverlay.AppCompat">

    <com.ahmedroid.bayttask.CustomToolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginBottom="50dp"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="120dp"
    app:layout_collapseMode="parallax"
    app:layout_scrollFlags="scroll|exitUntilCollapsed">


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

<android.support.design.widget.TabLayout
    android:id="@+id/tabLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    app:layout_scrollFlags="scroll|enterAlways"
    app:tabIndicatorColor="@color/error_color"
    app:tabSelectedTextColor="@color/error_color" />

    

<android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

正如您所看到的,我甚至试图创建基于它的自定义视图,但没有运气。

Java文件:

public class ProfileActivity extends AppCompatActivity {


private ViewPager mViewPager;
private TabLayout mTabLayout;
private PagerAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    CustomToolbar collapsingToolbarLayout = new CustomToolbar(this);
    collapsingToolbarLayout.setTitle("Ahmed Awad\n helloo");

    mViewPager = (ViewPager) findViewById(R.id.viewPager);
    mTabLayout = (TabLayout) findViewById(R.id.tabLayout);

    mTabLayout.addTab(mTabLayout.newTab().setText("Profile"));
    mTabLayout.addTab(mTabLayout.newTab().setText("Activity"));

    adapter = new PagerAdapter
            (getSupportFragmentManager(), mTabLayout.getTabCount());
    mViewPager.setAdapter(adapter);
    mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
    mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            mViewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_profile, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

我添加了断行符转义字符,但这不起作用,因为在API中它们将它设置为单行。

enter code here

0 个答案:

没有答案