Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将视图添加到工具栏_Android - Fatal编程技术网

Android 将视图添加到工具栏

Android 将视图添加到工具栏,android,Android,我搜索了这个问题,但还没有找到有用的答案。 我正在尝试创建一个工具栏,其中包含一些编辑文本。 应该是这样的: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:la

我搜索了这个问题,但还没有找到有用的答案。 我正在尝试创建一个工具栏,其中包含一些编辑文本。 应该是这样的:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/activity_main_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:elevation="8dp"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/activity_main_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/title"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:textSize="22sp"/>

        </LinearLayout>

    </android.support.v7.widget.Toolbar>
</LinearLayout>
private Toolbar toolbar;

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.acitivty_main);

  toolbar = (Toolbar)findViewbyid(R.id.activity_main_toolbar);

  setSupportActionbar(toolbar);

我的XML文件应该是什么样子?现在看起来是这样的:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/activity_main_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:elevation="8dp"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/activity_main_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/title"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:textSize="22sp"/>

        </LinearLayout>

    </android.support.v7.widget.Toolbar>
</LinearLayout>
private Toolbar toolbar;

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.acitivty_main);

  toolbar = (Toolbar)findViewbyid(R.id.activity_main_toolbar);

  setSupportActionbar(toolbar);
}

但结果是一些编辑文本正好位于工具栏的中心,没有空间放置工具栏标题(请忽略“保存”菜单按钮)

现在我的问题是,

  • 如何将一个或多个视图正确添加到主操作栏高度下方的工具栏
  • 你能给我推荐一些例子或教程页面吗
  • 在显示的带有浮动提示的图像中使用了哪种EditText

  • 提前感谢

    首先,我认为回答第二个和第三个问题很容易,您可以在“Android设计支持库”的“Android开发者博客”中找到这些信息。链接如下:

    只需向下滚动,直到找到名为“编辑文本的浮动标签”的部分并进行排序

    对于第一个问题,我做的是做了一个“工具栏”布局(因为我现在很忙,所以在此无法详细说明),然后将其添加到布局中。代码如下:

    工具栏布局:

    <?xml version="1.0" encoding="utf-8"?>
    <!-- NOTE: Use To Maintain Structure Of Actionbar -->
    <Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:background="#009688"
        android:id="@+id/toolBar"
        android:elevation="2dp">
        <!-- NOTE: Required For Top Section Structure -->
        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:orientation="vertical">
            <EditText
                android:background="@android:color/transparent"
                android:hint="@string/note_title"
                android:textColorHint="#FFFFFF"
                android:id="@+id/actionTitle"
                android:paddingRight="16dp"
                style="@style/ActionText"/>
            <TextView
                android:id="@+id/textDate"
                style="@style/ActionDate"/>
        </LinearLayout>
    </Toolbar>
    
    
    
    将其添加到布局文件中

    <?xml version="1.0" encoding="utf-8"?>
    <!-- NOTE: Layout For Edit Note Activity -->
    <RelativeLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:fitsSystemWindows="true">
        <!-- NOTE: Assigned Custom Toolbar -->
        <include
            android:id="@+id/toolBar"
            layout="@layout/tool"/>
    </RelativeLayout>
    
    
    
    这应该会帮你解决问题(然后你可以乱搞actionbar属性来清理它,也就是getActionBar().setElevation(0);以及 getActionBar().setTitle(“”)