Android: 操作栏内的 AppCompatActivity 和 textview
Android: AppCompatActivity and textview inside the action bar
我想在 AppCompatActivity 的操作栏中添加一个 autocompletetextview 组件。我的代码是:
styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread.setDefaultUncaughtExceptionHandler(new GlobalException());
Toolbar tb = (Toolbar) findViewById(R.id.toolbar_search);
setSupportActionBar(tb);
}
layout/action_bar_search.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar_search"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways"
app:layout_collapseMode="pin">
<AutoCompleteTextView
android:id="@+id/theSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/search_the"
android:imeOptions="actionSearch"
android:inputType="textAutoComplete|textAutoCorrect"
android:textColor="#FFFFFF" >
<requestFocus />
</AutoCompleteTextView>
</android.support.v7.widget.Toolbar>
当我的应用程序打开时,我看到一个空白的白色屏幕,几秒钟后,它变成一个空白的黑色屏幕,一直保持这种状态,直到我手动停止它。有什么想法吗?
显然,Toolbar tb = (Toolbar) findViewById(R.id.toolbar_search);
tb 为空后
下面是 AutocompleteTextView 的示例代码:
AutoCompleteTextView text;
String[] languages={"Android ","java","IOS","SQL","JDBC","Web services"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text=(AutoCompleteTextView)findViewById(R.id.theSearch);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,languages);
text.setAdapter(adapter);
text.setThreshold(1);
}
希望对你有所帮助....
您的 Toolbar
XML 未包含在您的 R.layout.activity_main
中,因此 findById
将不起作用。这会导致 Toolbar
保持为空并使您的应用程序崩溃。您可以使用 <include>
标签包含布局资源。在你的情况下:<include layout="@layout/action_bar_search"/>
.
我想在 AppCompatActivity 的操作栏中添加一个 autocompletetextview 组件。我的代码是:
styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread.setDefaultUncaughtExceptionHandler(new GlobalException());
Toolbar tb = (Toolbar) findViewById(R.id.toolbar_search);
setSupportActionBar(tb);
}
layout/action_bar_search.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar_search"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways"
app:layout_collapseMode="pin">
<AutoCompleteTextView
android:id="@+id/theSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/search_the"
android:imeOptions="actionSearch"
android:inputType="textAutoComplete|textAutoCorrect"
android:textColor="#FFFFFF" >
<requestFocus />
</AutoCompleteTextView>
</android.support.v7.widget.Toolbar>
当我的应用程序打开时,我看到一个空白的白色屏幕,几秒钟后,它变成一个空白的黑色屏幕,一直保持这种状态,直到我手动停止它。有什么想法吗?
显然,Toolbar tb = (Toolbar) findViewById(R.id.toolbar_search);
tb 为空后
下面是 AutocompleteTextView 的示例代码:
AutoCompleteTextView text;
String[] languages={"Android ","java","IOS","SQL","JDBC","Web services"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text=(AutoCompleteTextView)findViewById(R.id.theSearch);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,languages);
text.setAdapter(adapter);
text.setThreshold(1);
}
希望对你有所帮助....
您的 Toolbar
XML 未包含在您的 R.layout.activity_main
中,因此 findById
将不起作用。这会导致 Toolbar
保持为空并使您的应用程序崩溃。您可以使用 <include>
标签包含布局资源。在你的情况下:<include layout="@layout/action_bar_search"/>
.