【问题标题】:Android v7 Toolbar button alignmentAndroid v7 工具栏按钮对齐
【发布时间】:2015-04-02 17:25:40
【问题描述】:

我使用下面的代码在我的应用程序中添加了android.support.v7.widget.Toolbar,现在我想在工具栏的右端显示一个按钮,但无法这样做。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/accent_color"
    android:minHeight="?attr/actionBarSize"
    android:layout_alignParentTop="true"
    tools:context=".MyActivity"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/showevents"
        android:textSize="12sp"
        android:background="@null"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:textColor="@color/white"
        android:text="UPCOMING \nEVENTS"/>
</android.support.v7.widget.Toolbar>

我也添加了下图,但它没有向右移动。

android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"

附图供参考:

【问题讨论】:

    标签: android android-layout android-actionbar android-actionbar-compat android-toolbar


    【解决方案1】:

    你应该为你的按钮添加android:layout_gravity="end"

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:id="@+id/showevents"
            android:textSize="12sp"
            android:background="@null"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:textColor="#FFF"
            android:text="UPCOMING \nEVENTS"/>
    

    【讨论】:

    • 最好将按钮的样式设置为style="@style/Widget.AppCompat.Button.Borderless" 或使用background="?android:attr/selectableItemBackground" 而不是@null 背景,以在触摸时产生涟漪效果,否则按钮似乎被禁用。
    • 提示:可以使用android:textAllCaps="true" 而不是自己写全部大写android:text。 Material 主题按钮会自动包含它,但在旧设备上,有非大写按钮是正常的。
    • @Roel, ?selectableItemBackgroundBorderless 两者兼得。
    • 使用“end”而不是“right”(这就是 Android Studio 在 2018 年警告我的内容)
    • 如果我以编程方式添加一个视图会怎样,在我的情况下 View v1 = inflate(R.layout.view1); v1.addRule(ALIGN_END, TURE); getToolbar().addView(v1);不起作用。请帮忙,谢谢。
    【解决方案2】:

    或者右上角的图片:

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:minHeight="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:title="Edit Note">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:id="@+id/submitEditNote"
            android:src="@android:drawable/ic_menu_send"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true" />
    </android.support.v7.widget.Toolbar>
    

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      你的方法是对的,但是看看官方文档:(Resourcs)

      创建项目菜单:

      <!-- "Mark Favorite", should appear as action button if possible -->
      <item
          android:id="@+id/action_favorite"
          android:icon="@drawable/ic_favorite_black_48dp"
          android:title="@string/action_favorite"
          app:showAsAction="ifRoom"/>
      
      <!-- Settings, should always be in the overflow -->
      <item android:id="@+id/action_settings"
            android:title="@string/action_settings"
            app:showAsAction="never"/>
      

      按 ID 添加菜单操作

      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
              case R.id.action_settings:
                  // User chose the "Settings" item, show the app settings UI...
                  return true;
      
              case R.id.action_favorite:
                  // User chose the "Favorite" action, mark the current item
                  // as a favorite...
                  return true;
      
              default:
                  // If we got here, the user's action was not recognized.
                  // Invoke the superclass to handle it.
                  return super.onOptionsItemSelected(item);
      
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-29
        • 2014-09-13
        • 2016-04-22
        • 2020-12-01
        相关资源
        最近更新 更多