【发布时间】:2018-03-01 18:06:35
【问题描述】:
首先,我知道这里的答案太多了。但我可能都试过了。 这是链接:
How to display custom view in ActionBar?
How to make a Custom View(EditText) on the actionbar automatically selected?
其他一些网站:
http://wptrafficanalyzer.in/blog/adding-custom-action-view-to-action-bar-in-android/
http://www.sanfoundry.com/java-android-program-add-custom-view-actionbar/
我想制作一个具有 Edittext 的自定义操作栏。这是我的代码。 但我所有的尝试都是失败的。 Emulatoror 或真正的手机粉碎了我所有的运行。我该如何解决它?
我的 main_activty :
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> data = new ArrayList<>();
data.add("hi");
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
actionBarCustomView = findViewById(R.id.actionbar_in_toolbar); //wrapper because layout is included
autocompleteTextView = actionBarCustomView.findViewById(R.id.actionbar_search);
autocompleteTextView.clearFocus();
autocompleteTextView.setAdapter(new AutocompleteListViewAdapter(this, R.layout.activity_main, data, this));
}
}
我的 layout_part_of_design :缺少课程
找不到以下类: - android.support.design.widget.AppBarLayout(修复构建路径、编辑 XML、创建类) - android.support.design.widget.CoordinatorLayout(修复构建路径、编辑 XML、创建类) 提示:尝试构建项目。提示:尝试刷新布局。
没有任何代码或不同的东西。我使用的是 21 API。
和渲染问题:
无法解析资源@color/primary 提示:尝试刷新布局。
Activity_main xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="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">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@color/primary"
android:focusable="true"
android:focusableInTouchMode="true"
android:paddingBottom="10dp"
android:paddingEnd="20dp"
android:paddingRight="20dp"
android:paddingTop="10dp">
<AutoCompleteTextView
android:id="@+id/actionbar_search"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:hint="Some Hint"
android:background="@color/primary_dark"
android:drawableLeft="@drawable/ic_search_white_24dp"
android:drawableStart="@drawable/ic_search_white_24dp"
android:gravity="center_vertical"
android:padding="5dp"
android:singleLine="true"
android:textColor="@color/white"
android:textColorHint="@color/white"
android:textSize="@dimen/bigger_text_size_14sp" />
</RelativeLayout>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">
<include
android:id="@+id/actionbar_in_toolbar"
layout="@layout/toolbar.xml" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:background="@android:color/white"
tools:ignore="MergeRootFrame" />
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
【问题讨论】:
-
您是否尝试使用自定义工具栏而不是我认为这样会更好
-
只是我试图将edittext放入ActionBar。之后我正在使用它。另一方面,自定义工具栏是什么?
-
您究竟是如何实现这一目标的?您是否创建了一个额外的 xml 布局,在其中放置了一个 edittext 并尝试将该 xml 作为您的操作栏?我发现很难理解您实施的技术,因为我看不到代码与操作栏本身有任何联系。如果您能告诉我您想要实现的目标,我可以尝试为您指明正确的方向。
-
我想在 ActionBar 中放一个 AutoCompleteTextView。我的目的是为了给我建议。 (我说 Edittext 因为有很多资源)顺便说一句;这些代码不是我的。