【问题标题】:Android action bar above buttons按钮上方的 Android 操作栏
【发布时间】:2016-11-01 17:19:41
【问题描述】:

这是我的布局。现在我有了我的工作工具栏,但按钮在工具栏下方。我尝试在这里和那里添加一些边距,但由于某种原因,一个按钮总是与其他按钮不对齐,即使我总是使用相同的 MarginTop。我该如何解决这个问题?

<android.support.v7.widget.Toolbar
   android:id="@+id/my_toolbar"
   android:layout_width="match_parent"
   android:layout_height="?attr/actionBarSize"
   android:background="?attr/colorPrimary"
   android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
   app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Start"
       android:id="@+id/button"
       android:layout_alignTop="@+id/buttonP"
       android:layout_centerHorizontal="true"
       app:layout_widthPercent="40%"
       app:layout_marginTopPercent="2%"/>

<Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="&lt;&lt;"
       android:id="@+id/buttonP"
       android:layout_alignParentLeft="true"
       app:layout_widthPercent="30%"
   app:layout_marginTopPercent="2%"/>

    <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="&gt;&gt;"
       android:id="@+id/buttonN"
       android:layout_above="@+id/custom_list"
       android:layout_alignRight="@+id/custom_list"
       app:layout_widthPercent="30%"
       app:layout_marginTopPercent="2%"/>

    <ListView
       android:id="@+id/custom_list"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:dividerHeight="1dp"
       android:layout_weight="0.5"
       android:layout_below="@+id/button" />
</android.support.percent.PercentRelativeLayout>

【问题讨论】:

    标签: android xml android-layout android-actionbar


    【解决方案1】:

    对于工具栏,您应该使用线性布局,然后是按钮。 您还需要 layout_weight 属性来正确对齐每个按钮。 一个例子:

     <android.support.v7.widget.Toolbar
                android:id="@+id/toolBar"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_gravity="start|center_vertical"
                android:background="@color/grey"
                app:popupTheme="@style/Theme.App.PopupOverlay" />
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/white"
                android:orientation="horizontal"
                android:weightSum="10">
    
                <RelativeLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1.3"
                    android:padding="5dp">
    
                    <ImageView
                        android:id="@+id/iv"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:background="@drawable/menu_ic"
                        android:padding="5dp" />
                </RelativeLayout>
    
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="start|center_vertical"
                    android:layout_weight="1"
                    android:text="@string/xyz"
                    android:textColor="@color/darker_gray"
                    android:textSize="18sp" />
    
                <View
                    android:layout_width="1dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="start|center_vertical"
                    android:layout_marginBottom="10dp"
                    android:layout_marginTop="10dp"
                    android:background="@color/grey" />
    
                <EditText
                    android:id="@+id/et_search"
                    android:layout_width="0dp"
                    android:layout_height="40dp"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp"
                    android:layout_weight="6.4"
                    android:background="@android:color/transparent"
                    android:gravity="start|center_vertical"
                    android:hint="@string/search_keyword"
                    android:singleLine="true"
                    android:textColor="@color/darker_gray"
                    android:textColorHint="@color/darker_gray"
                    android:textSize="17sp" />
    
                <View
                    android:layout_width="1dip"
                    android:layout_height="match_parent"
                    android:layout_gravity="end"
                    android:background="@color/grey" />
    
                <RelativeLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1.3">
    
                    <ImageView
                        android:id="@+id/notifications"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:background="@drawable/notification_icon" />
    
                </RelativeLayout>
    
            </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      添加操作栏按钮

      为要包含在操作栏中的每个项目添加一个&lt;item&gt; 元素,如以下菜单 XML 文件的代码示例所示:

      <menu xmlns:android="http://schemas.android.com/apk/res/android" >
      
          <!-- "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"/>
      
      </menu>
      

      app:showAsAction 属性指定操作是否应在应用栏上显示为按钮。如果您设置 app:showAsAction="ifRoom"(如示例代码中最喜欢的操作),如果应用栏中有空间,该操作将显示为按钮;如果没有足够的空间,多余的动作会被发送到溢出菜单。如果您设置 app:showAsAction="never"(如示例代码的设置操作),则该操作始终列在溢出菜单中,而不显示在应用栏中。

      Sample link

      【讨论】:

      • 我在另一个 xml 文件中有这段代码,实际上我的操作栏可以工作。我的问题可能出在布局上。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-08
      相关资源
      最近更新 更多