android 用toolbar实现标题栏

img


上图这样的布局如何用Toolbar实现。下面是我的实现达不到想要的分布效果,文字挤在一块了


 <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"

        
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" >


        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@drawable/ic_home_black_24dp" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="24dp"
            android:layout_weight="3"
            android:text="返回"
            android:textColor="@color/black"
            android:gravity="center"/>

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="24dp"
            android:layout_weight="15"
            android:text="个人信息"
            android:textColor="@color/black"
            android:gravity="center"
            />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="24dp"
            android:layout_weight="3"
            android:text="帮助"
            android:textColor="@color/black"
            android:gravity="center"/>
    </androidx.appcompat.widget.Toolbar>

结果如下图(文字都挤到一块了)

img

Toolbar是通过添加菜单的方式添加按钮的,你这样直接在上面堆组件,肯定不行

  • 你可以看下这个问题的回答https://ask.csdn.net/questions/263153
  • 这篇博客也不错, 你可以看下Android 修改Toolbar自带的图标颜色
  • 你还可以看下android参考手册中的 android Toolbar 在应用程序内容中使用的标准工具条。
  • 除此之外, 这篇博客: Android 美化之Toolbar控件使用中的 二、在布局文件中写入Toolbar控件 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:id="@+id/toolbar"
            android:background="@color/colorPrimary"
            app:title="hello"
    
            android:layout_height="?attr/actionBarSize">
    
        </android.support.v7.widget.Toolbar>
    

    这里的高度设置成了actionBar的大小

  • 您还可以看一下 刘桂林老师的Android大型实战:《亲信,优雅的从入门到进阶》课程中的 ToolBar小节, 巩固相关知识点
  • 以下回答由chatgpt基于相关博客总结生成:

    可以使用自定义的Toolbar来实现这样的标题栏布局。具体步骤如下:

    1. 首先,在xml布局文件中使用自定义的Toolbar,类似于以下代码:
    <com.example.app.TitleCenterToolbar
      android:id="@+id/toolbar"
      android:layout_width="match_parent"
      android:layout_height="?attr/actionBarSize"
      android:background="@color/colorPrimary"
      app:navigationIcon="@drawable/ic_arrow_back_white_24dp" />
    

    这里的TitleCenterToolbar是自定义的Toolbar,app:navigationIcon属性用于设置左上角返回按钮的图标。

    1. 创建TitleCenterToolbar类,继承Toolbar类,并在其中初始化布局文件,类似于以下代码:
    public class TitleCenterToolbar extends Toolbar {
      private TextView mTitleTextView; // 标题文字View
    
      // 构造方法
    
      public TitleCenterToolbar(Context context) {
        this(context, null);
      }
    
      public TitleCenterToolbar(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.toolbarStyle);
      }
    
      public TitleCenterToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
      }
    
      // 初始化布局,找到标题文字View
    
      private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.layout_title_center_toolbar, this);
        mTitleTextView = (TextView) findViewById(R.id.tv_title);
      }
    
      // 重写setTitle方法,设置标题文字
    
      @Override
      public void setTitle(CharSequence title) {
        mTitleTextView.setText(title);
      }
    }
    

    这里的布局文件layout_title_center_toolbar.xml中只有一个TextView,用于显示标题文字。

    1. 在Activity中找到Toolbar,并使用setSupportActionBar方法将其设置为ActionBar:
    TitleCenterToolbar toolbar = (TitleCenterToolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    
    1. 在Activity中设置标题文字,类似于以下代码:
    setTitle("标题");
    

    这里的setTitle方法是继承自Activity类的方法,会调用Toolbar的setTitle方法。

    至此,就可以通过自定义Toolbar来实现这样的标题栏布局了。关于标题文字对齐方式,可以在TextView中添加android:gravity="center"属性来让文字居中显示。需要注意的是,在设置左上角返回按钮的图标时,应该使用app:navigationIcon属性来设置,而不是直接在布局文件中添加ImageView,因为这样可以保证返回按钮与系统默认的返回按钮一致,同时也会自动添加返回事件处理。