Android浏览器的操作栏

时间:2017-07-23 14:52:54

标签: android android-edittext android-actionbar menuitem

我正在创建一个个人浏览器app.i想要在操作栏上添加edittext,back_button,forward_button和home_button。但是当我在操作栏上设置EditText时,它就不可见了。然后我删除了操作栏并设置工具栏以使其运行良好。但是当我在其中设置back_button,forward_button和home_button时,它是不可见的。然后我再次添加操作栏。但它现在看起来像下面的截图。我的鳕鱼有什么问题?请建议我。这是我的截图 enter image description here

我想这样设置 enter image description here

这是我的style.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

</resources>

这是我的toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
    <EditText
        android:layout_width="190dp"
        android:layout_height="wrap_content"
        android:id="@+id/myEditText"
        android:hint="search"
        android:background="#ffffff" />
</android.support.v7.widget.Toolbar>
</LinearLayout>

这是我的edittext.xml(额外)

EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/action_address_bar"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:hint="MY"
android:imeOptions="actionSearch"
android:inputType="textUri" >
</EditText>

这是我的menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">


<item
    android:id="@+id/menu_search"
    app:showAsAction="always"
    android:title="Search"
    android:actionLayout="@layout/edit">
</item>

<item
    android:id="@+id/item_home"
    android:icon="@drawable/ic_action_home"
    android:title="Home"
    app:showAsAction="always"></item>

<item
    android:id="@+id/item_pre"
    android:icon="@drawable/ic_action_back"
    android:title="Back"
    app:showAsAction="always"></item>

<item
    android:id="@+id/item_forw"
    android:icon="@drawable/ic_action_forw"
    android:title="Forward"
    app:showAsAction="always"></item>



</menu>

这是我的mainActivity.java

public class MainActivity extends AppCompatActivity {
Toolbar  mToolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu,menu);
    return super.onCreateOptionsMenu(menu);
}

private void initToolbar() {
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    mToolbar.setTitleTextColor(Color.WHITE);
    mToolbar.setTitle("gnappo");
    mToolbar.showOverflowMenu();
    setSupportActionBar(mToolbar);
}
}

感谢提前

2 个答案:

答案 0 :(得分:2)

这里你去解决你的问题

<强> MainActivty.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="match_parent"
    tools:context="com.pro.salman.toolbar.MainActivity">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimaryDark"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:id="@+id/toolbar">


        <EditText
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:hint="Search"
            android:visibility="invisible"
            android:singleLine="true"/>

    </android.support.v7.widget.Toolbar>

</RelativeLayout>

<强> menu.xml文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:title="Forward"
        android:id="@+id/forward"
        android:icon="@drawable/ic_arrow_forward_white_24dp"
        app:showAsAction="always"
        android:orderInCategory="100"/>



    <item
        android:title="Backward"
        android:id="@+id/backward"
        android:icon="@drawable/ic_arrow_back_black_24dp"
        app:showAsAction="always"
        android:orderInCategory="90"/>

    <item
        android:title="Home"
        android:id="@+id/Home"
        android:icon="@drawable/ic_home_white_24dp"
        app:showAsAction="always"
        android:orderInCategory="80"/>



    <item
        android:title="Search"
        android:id="@+id/search"
        android:icon="@drawable/ic_search_white_24dp"
        app:showAsAction="always"
        android:orderInCategory="70"/>

</menu>

<强> MainActivity.java

public class MainActivity extends AppCompatActivity {

    private EditText mEditText;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar)(findViewById(R.id.toolbar));
        mEditText = (EditText)(findViewById(R.id.editText));
        setSupportActionBar(toolbar);
        setTitle("");

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if(id == R.id.search)
        {
            mEditText.setVisibility(View.VISIBLE);
        }

        return super.onOptionsItemSelected(item);
    }
}

现在结果

当用户按下编辑文本将显示的搜索按钮时,隐藏编辑文本

enter image description here

when serach icon is pressed

答案 1 :(得分:2)

Salman500的答案是最好的答案。只需在res中更改它们&gt;值&GT;这样的风格: -

 <style name="Appname" parent="Them.AppCompat.light.NoActionBar" 

并享受。