设置android'的问题;多次显示应用程序栏/工具栏标题

设置android'的问题;多次显示应用程序栏/工具栏标题,android,android-toolbar,android-appbarlayout,Android,Android Toolbar,Android Appbarlayout,我正在开发一个android应用程序,我一直在组织输入表单活动中的布局。我想像这个示例一样组织我的活动布局(可以在谷歌android的文档中找到):。因此,一个大的应用程序栏将包含一个或两个文本字段,引用用户活动的名称和描述,并在活动布局的内容中包含其余的输入控件。 我的行为是: 活动标题最初为空,工具栏已折叠 当我按下工具栏时,会出现文本输入,用户会插入一些文本 当用户折叠工具栏时,活动标题(即工具栏标题)必须设置为输入文本值 问题是,这种机制只工作一次:一旦设置值并折叠工具栏,当我再次按下工

我正在开发一个android应用程序,我一直在组织输入表单活动中的布局。我想像这个示例一样组织我的活动布局(可以在谷歌android的文档中找到):。因此,一个大的应用程序栏将包含一个或两个文本字段,引用用户活动的名称和描述,并在活动布局的内容中包含其余的输入控件。 我的行为是:

  • 活动标题最初为空,工具栏已折叠
  • 当我按下工具栏时,会出现文本输入,用户会插入一些文本
  • 当用户折叠工具栏时,活动标题(即工具栏标题)必须设置为输入文本值
  • 问题是,这种机制只工作一次:一旦设置值并折叠工具栏,当我再次按下工具栏,设置文本输入值并折叠工具栏时,标题未设置,即setTitle在第一次没有效果

    现在我将列出我的代码:我尝试以以下方式使用自定义应用程序栏布局、折叠工具栏布局、工具栏和文本输入:

    <?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="wrap_content"
        android:orientation="vertical"
        tools:context="com.example.davide.inputformapp.MainActivity">
    
        <com.example.davide.inputformapp.MyAppBarLayout.MyAppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
            <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleMarginEnd="@dimen/activity_horizontal_margin"
                app:expandedTitleMarginStart="@dimen/activity_horizontal_margin"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="parallax"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    
                <android.support.design.widget.TextInputEditText
                    android:id="@+id/textInput"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
    
            </android.support.design.widget.CollapsingToolbarLayout>
    
        </com.example.davide.inputformapp.MyAppBarLayout.MyAppBarLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    
    主活动的onCreate()方法中的代码是:

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    textInput = (TextInputEditText) findViewById(R.id.textInput);
    final MyAppBarLayout collapsableAppBar = (MyAppBarLayout) findViewById(R.id.appbar);
    
    setSupportActionBar(toolbar);
    setTitle(null);
    
    collapsableAppBar.setOnStateChangeListener(new MyAppBarLayout.OnStateChangeListener() {
        @Override
        public void onStateChange(MyAppBarLayout.State toolbarChange) {
            String value = textInput.getText().toString();
            toolbar.setTitle(value);
        }
    });
    
    有什么建议吗

    谢谢, 大卫

    请尝试更换

    toolbar.setTitle(value) 
    


    中所述,您必须更改标题,如下所示: 而不是

    toolbar.setTitle(值)

    getSupportActionBar.setTitle(值)


    setOnStateChangeListener

    这可能不是一个好的解决方案,但让每件事情都正常工作的一种方法是创建自定义操作栏 custom_actionbar.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@drawable/yourcolorforactionbar" >
    
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textAllCaps="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#fff"
            android:textStyle="bold" />
    
        <ImageView
            android:id="@+id/myimage"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="8dp"
            android:src="@drawable/yourimagetoset" />
    
        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="8dp"
            android:background="@null"
            android:src="@android:drawable/menubutoon" />
    
    </RelativeLayout>
    
    将“android:id”设置为折叠工具栏布局。在“活动”中找到它并为其设置标题。
    我尝试了很多答案,但只将标题设置为CollasingToolbarLayout有效。

    可能您的
    onStateChange
    没有被调用?您是否尝试在那里设置断点?是的,已到达断点!但是setTitle()方法无效。您必须像下面这样更改标题
    getSupportActionBar.setTitle(值)
    on
    setOnStateChangeListener
    getSupportActionBar().setTitle(value)
    
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@drawable/yourcolorforactionbar" >
    
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textAllCaps="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#fff"
            android:textStyle="bold" />
    
        <ImageView
            android:id="@+id/myimage"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="8dp"
            android:src="@drawable/yourimagetoset" />
    
        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="8dp"
            android:background="@null"
            android:src="@android:drawable/menubutoon" />
    
    </RelativeLayout>
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ActionBar mActionBar = getActionBar();
            mActionBar.setDisplayShowHomeEnabled(false);
            mActionBar.setDisplayShowTitleEnabled(false);
            LayoutInflater mInflater = LayoutInflater.from(this);
    
            View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
            TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
            mTitleTextView.setText("My Own Title");
    
            ImageButton imageButton = (ImageButton) mCustomView
                    .findViewById(R.id.imageButton);
            imageButton.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View view) {
                    Toast.makeText(getApplicationContext(), "Refresh Clicked!",
                            Toast.LENGTH_LONG).show();
                }
            });
    
            mActionBar.setCustomView(mCustomView);
            mActionBar.setDisplayShowCustomEnabled(true);
        }
    
    }