android - 使标题(工具栏)在android中固定

标签 android android-toolbar

我写了一个安卓应用(Material风格)。
这是代码:
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">


    <android.support.design.widget.AppBarLayout 
        android:layout_height="wrap_content"
        android:layout_width="match_parent" 
        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" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />
        <!--android:layout_toEndOf="@id/fab"
        android:layout_toRightOf="@id/fab"-->


</android.support.design.widget.CoordinatorLayout>  

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main" tools:context=".MainActivity">

    <TextView
        android:text="@string/sample_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <!--My layout-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/place_holder"
            android:textEditSuggestionItemLayout="@color/colorAccent"
            android:layout_gravity="bottom"
            android:layout_weight="1"
            />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="@string/btn_name"
            android:textEditSuggestionItemLayout="@color/colorAccent"
            android:layout_gravity="bottom"
            />

    </LinearLayout>

</RelativeLayout>  

Java代码:

package com.enexl.ajaykulkarni_enexl.omgandroid;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import static android.widget.Toast.*;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();*/
                Context context = getApplicationContext();
                String text = "Let us create an action now!";
                int duration = Toast.LENGTH_SHORT;

                Toast toast = Toast.makeText(context,text,duration);
                toast.show();
            }
        });
    }

//    private Toast makeText(Context context, CharSequnce text, int duration) {
//    }

    @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) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}  

结果:
img
当我单击 editText 字段时,弹出键盘并且标题(顶部的工具栏)不可见。在此屏幕截图中查看:
img2
我希望标题(顶部的工具栏)在键盘弹出时保持在 View 中。我该怎么做?

最佳答案

我在我的 list Activity 中试过这个:

android:windowSoftInputMode="stateVisible|adjustResize" 

它奏效了。

关于android - 使标题(工具栏)在android中固定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33229562/

相关文章:

android - 标签栏和抽屉导航对齐

android - 在 Lollipop 前设备的工具栏上添加高程/阴影

android - 操作栏、工具栏、 Activity 继承

java - Android KitKat 图像选择不返回任何东西

android - 无法从缓存 okHttp 和改造中加载数据

android - PhoneStateListener Android 错误值

android - 工具栏布局未定义

java - 渐进式图像渲染,逐像素加载图像并在 imageLoader 中更新 ImageView

Android:向 TextView 添加一个额外的字符串,该字符串从数据库填充其内容

android - 如何将所有操作项放在左侧,尽可能多地占用空间,同时在右侧溢出?