settitle() 在 android 按钮 clicklistner 中不起作用
settitle() is not working in android on button clicklistner
我使用 setActionBarTitle();
更改操作栏的标题它在 onCreate()
方法中工作正常但是当我想在按钮点击侦听器上实现它时它不起作用
public class ScrollingActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
toolbar = (Toolbar) findViewById(R.id.toolbar);
CollapsingToolbarLayout mToolbarlayout= (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
setSupportActionBar(toolbar);
Button newbutton= (Button) findViewById(R.id.changetitle);
if (newbutton != null) {
newbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ScrollingActivity.this, "Hi", Toast.LENGTH_SHORT).show();
setActionBarTitle("HI");
}
});
}
}
public void setActionBarTitle(String title) {
//noinspection ConstantConditions
getSupportActionBar().setTitle(title); //check not working
}
}
xml用于activity,我想更改其中的标题
<?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="appcom.taskremainder.calender.ScrollingActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
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="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"
android:src="@android:drawable/ic_menu_edit"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end" />
</android.support.design.widget.CoordinatorLayout
>
是的,你是对的,在 onCreate() 中,ActionBar/ToolBar 的标题发生了变化,如果我们调用该方法来更改按钮上的标题,则单击它不会。
按钮定义在:
<include layout="@layout/content_scrolling"/>
我尝试了很多不同的方法并偶然发现了这个问题https://code.google.com/p/android/issues/detail?id=77763
如果您想尝试(更改标题),我有一个解决方法
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
android.support.v7.app.ActionBar actionBar;
CollapsingToolbarLayout mToolbarlayout;
Button newButton;
// On every click I will change the value of i
static int i=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
mToolbarlayout= (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
newButton= (Button) findViewById(R.id.changetitle);
setSupportActionBar(toolbar);
actionBar=getSupportActionBar();
newButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//oldWay();
newWay();
}
});
}
private void oldWay() {
Toast.makeText(MainActivity.this, ""+actionBar.getTitle(), Toast.LENGTH_SHORT).show();
actionBar.setTitle("Something Else");
Toast.makeText(MainActivity.this/, ""+actionBar.getTitle(), Toast.LENGTH_SHORT).show();
//Toast says that value has changed, but it doesn't on screen
}
private void newWay() {
mToolbarlayout.setTitle("" + i++);
}
}
让我知道这是否有帮助。
我使用 setActionBarTitle();
更改操作栏的标题它在 onCreate()
方法中工作正常但是当我想在按钮点击侦听器上实现它时它不起作用
public class ScrollingActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
toolbar = (Toolbar) findViewById(R.id.toolbar);
CollapsingToolbarLayout mToolbarlayout= (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
setSupportActionBar(toolbar);
Button newbutton= (Button) findViewById(R.id.changetitle);
if (newbutton != null) {
newbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ScrollingActivity.this, "Hi", Toast.LENGTH_SHORT).show();
setActionBarTitle("HI");
}
});
}
}
public void setActionBarTitle(String title) {
//noinspection ConstantConditions
getSupportActionBar().setTitle(title); //check not working
}
}
xml用于activity,我想更改其中的标题
<?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="appcom.taskremainder.calender.ScrollingActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
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="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"
android:src="@android:drawable/ic_menu_edit"
app:layout_anchor="@id/app_bar"
app:layout_anchorGravity="bottom|end" />
</android.support.design.widget.CoordinatorLayout
>
是的,你是对的,在 onCreate() 中,ActionBar/ToolBar 的标题发生了变化,如果我们调用该方法来更改按钮上的标题,则单击它不会。
按钮定义在:
<include layout="@layout/content_scrolling"/>
我尝试了很多不同的方法并偶然发现了这个问题https://code.google.com/p/android/issues/detail?id=77763
如果您想尝试(更改标题),我有一个解决方法
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
android.support.v7.app.ActionBar actionBar;
CollapsingToolbarLayout mToolbarlayout;
Button newButton;
// On every click I will change the value of i
static int i=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
mToolbarlayout= (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
newButton= (Button) findViewById(R.id.changetitle);
setSupportActionBar(toolbar);
actionBar=getSupportActionBar();
newButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//oldWay();
newWay();
}
});
}
private void oldWay() {
Toast.makeText(MainActivity.this, ""+actionBar.getTitle(), Toast.LENGTH_SHORT).show();
actionBar.setTitle("Something Else");
Toast.makeText(MainActivity.this/, ""+actionBar.getTitle(), Toast.LENGTH_SHORT).show();
//Toast says that value has changed, but it doesn't on screen
}
private void newWay() {
mToolbarlayout.setTitle("" + i++);
}
}
让我知道这是否有帮助。