【问题标题】:How to set up Edittext to Actionbar?如何将 Edittext 设置为 Actionbar?
【发布时间】: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 因为有很多资源)顺便说一句;这些代码不是我的。

标签: android android-actionbar


【解决方案1】:

您肯定需要一个自定义工具栏。

首先,让我们为工具栏创建自定义 xml。

toolbar.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: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>

然后将此工具栏放在您的活动布局中(包括片段) activity_something.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">

        <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>

然后在活动中(用 Java 编写),设置为自定义工具栏,按 ID 查找视图并执行所有您需要的操作 MyActivity.java

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.item_some_layout, data, this));

在你的风格主题中设置它。

<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>

app.gradle

minSdkVersion  16
targetSdkVersion  22
compileSdkVersion  27
buildToolsVersion  '27.0.3'
...
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation com.android.support:design:25.3.1
...

这应该可以。

【讨论】:

  • 我已经编辑了我的问题。谢谢你的兴趣。而且我从来没有听说过工具栏。这就是我没有这样做的原因
  • 是的,试试吧,应该可以的。我已经从生产应用程序中复制了这段代码。如果可行,请投票并接受答案。谢谢
  • 很遗憾没有。再看看我的问题。
  • 您要求使用 AutoCompleteTextView。您应该在 gradle 中包含支持设计,而不是 @color/primary 您可以设置任何颜色。编辑了我的答案。跟着它走。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多