【发布时间】:2015-03-08 04:39:09
【问题描述】:
我想动态更改操作栏的外观。应用我之前创建的自定义视图布局,但仍然无法正常工作。这是我的代码:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar mActionBar = getSupportActionBar();
mActionBar.setCustomView(R.layout.activity_main);
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
}
@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_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
ActionBar mActionBar = getSupportActionBar();
Toast.makeText(this,item.toString(),Toast.LENGTH_LONG).show();
if(item.toString() == "search"){
mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
mActionBar.setCustomView(R.layout.search);
}
return super.onOptionsItemSelected(item);
}
自定义布局 xml
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
xmlns:android="schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:id="@+id/etSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
</LinearLayout>
【问题讨论】:
-
错误是什么?你能分享自定义操作栏布局吗
-
只是没有显示。编辑了帖子
-
你是说ActionBar没有渲染?我快速构建了您的代码,并且可以正常查看。您提到要“更改操作栏的外观”。您是说
ActionBar应该有一个可编辑的文本字段,但在按下 ActionBar 标题时什么都不做? -
完全正确:编辑文本不会出现