37

Tôi đã thêm android.support.v7.widget.Toolbar vào ứng dụng của mình bằng cách sử dụng mã bên dưới, bây giờ tôi muốn hiển thị nút ở đầu bên phải của thanh công cụ nhưng không thể làm như vậy.Android v7 Chỉnh sửa nút trên Thanh công cụ

<?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> 

Tôi đã thêm phần bên dưới được hiển thị nhưng không được chuyển sang phải.

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

đính kèm hình ảnh để tham khảo:

enter image description here

Trả lời

94

Bạn nên thêm android:layout_gravity="right" cho Button của bạn:

 <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="right" 
     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"/> 

enter image description here

+0

Better thiết lập các phong cách của các nút cho 'style = "@ style/Widget.AppCompat.Button.Borderless"' hoặc sử dụng 'background =" android:? Attr/selectableItemBackground "' thay vì một nền @null để có hiệu ứng gợn sóng khi chạm vào nếu không nút có vẻ bị tắt. – Roel

+1

Mẹo: 'android: textAllCaps =" true "' có thể được sử dụng thay vì viết tất cả các chữ cái 'android: text'. Các nút theo chủ đề vật chất tự động bao gồm, nhưng trên các thiết bị cũ thì việc sử dụng các nút không phải chữ hoa là điều bình thường. – TWiStErRob

+1

@Roel, '? SelectableItemBackgroundBorderless' để có cả hai. – WindRider

5

Hoặc cho hình ảnh phía trên bên phải:

<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> 

Tôi hy vọng nó giúp

0

cách của bạn là đúng, nhưng hãy nhìn vào doc chính thức: (Resourcs)

Tạo menu các mục:

<!-- "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"/> 

Thêm hành động trình đơn bằng 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); 

    } 
} 
Các vấn đề liên quan