【问题标题】:Creating a custom toolbar in android在android中创建自定义工具栏
【发布时间】:2015-05-29 16:00:39
【问题描述】:

我正在尝试在 android 中创建一个自定义扩展工具栏,并在工具栏中添加一个编辑文本。我想要实现的布局看起来像这样

我编写的实现代码是这样的:

<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"
android:paddingBottom="@dimen/activity_vertical_margin"    tools:context=".MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/my_awesome_toolbar"
    android:layout_height="256dp"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"

    >

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/searchbox"
        android:layout_alignParentBottom="true"
        android:text="Test"
        android:background="#ffffff"
        />

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

而Activity有如下代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
    if (toolbar != null) {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayShowHomeEnabled(false);
    }}

但我得到的是:

关于自定义扩展工具栏的教程并不多,因此非常感谢一些帮助。

【问题讨论】:

标签: android android-layout android-toolbar


【解决方案1】:

我认为您只需要在工具栏设置上添加重力=“底部”,例如:

   <android.support.v7.widget.Toolbar
    android:id="@+id/my_awesome_toolbar"
    android:gravity="bottom"
    android:layout_height="256dp"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary">

我必须在布局的底部添加一些边距才能使编辑出现,但这应该使文本位于编辑的底部。

或者您可以在 EditText 上设置 layout_gravity。

        <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_gravity="bottom"
        android:id="@+id/searchbox"
        android:text="Test"
        android:background="#ffffff"/>

我很惊讶 alignParentBottom 编译。我不相信 Toolbar 继承自 RelativeLayout。

编辑 - 这是我的完整布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">

<android.support.v7.widget.Toolbar
    android:id="@+id/my_awesome_toolbar"
    android:layout_height="264dp"
    android:layout_width="match_parent"
    android:layout_alignParentBottom="true"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary">

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_gravity="bottom"
        android:id="@+id/searchbox"
        android:text="Test"
        android:background="#ffffff"/>
</android.support.v7.widget.Toolbar>
</RelativeLayout>

结果如下:

【讨论】:

【解决方案2】:
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="?attr/colorPrimary">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginRight="20dp"
        android:background="@android:color/white">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:background="@null"
            android:inputType="text"
            android:textSize="22sp" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:padding="12dp"
            android:src="@drawable/ic_search" />
    </RelativeLayout>
</android.support.v7.widget.Toolbar>

【讨论】:

  • 请为您的答案添加一些解释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-10
  • 1970-01-01
  • 2019-02-25
  • 2017-09-30
相关资源
最近更新 更多