10

Tôi đã hỏi một câu hỏi tương tự here ... Tôi nhận được một số hướng dẫn trong câu trả lời. Nhưng câu hỏi này là diffrenet. bởi vì không có phương pháp nào không hoạt động trong dự án của tôi.cách căn giữa các biểu tượng trên thanh công cụ trong studio android

Tôi muốn căn giữa tất cả các biểu tượng trong thanh công cụ

Tôi kiểm tra tất cả các cách availabe ... nhưng luôn luôn biểu tượng trôi nổi ở bên trái.

Tôi muốn tập trung các biểu tượng (Tôi thích trái biểu tượng nổi trái và biểu tượng bên phải nổi phải và biểu tượng khác nổi trung tâm)

Activity.Main.xml

<LinearLayout 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" 
    android:orientation="vertical" // I add vertical 
    tools:context=".MainActivity"> 

    <include 
     android:id="@+id/tool_bar" 
     layout="@layout/tool_bar" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" // I add match_parent 
     android:layout_gravity="center" /> // I add center 
</LinearLayout>. 

Cũng trong tool_bar.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar 
    android:layout_height="wrap_content" 
    android:layout_height="match_parent" 
    android:background="@color/ColorPrimary" 
    android:elevation="2dp" 
    android:theme="@style/Base.ThemeOverlay.AppCompat.Dark" 
    android:layout_gravity="center" // I add center 
    xmlns:android="http://schemas.android.com/apk/res/android" /> 

in main_menu.xml

<menu 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" tools:context=".MainActivity"> 

<item 
    android:id="@+id/action_forward" 

    android:title="forward" 
    android:icon="@drawable/ic_action_arrow_forward" 
    app:showAsAction="always" 
    ></item> 


<item 
    android:id="@+id/action_zoom_in" 
    android:title="zoom in" 
    android:icon="@drawable/ic_action_zoom_in" 
    app:showAsAction="always" 
    ></item> 
<item ... 

và cho mã trên (main_menu.xml) trong <item> Tôi đã thử tất cả các mã:

android:layout_width="match_parent" 
android:layout_weight=".2" // 20% 
android:gravity="center" 
android:layout_centerVertical="true" 
android:layout_centerHorizontal="true" 

Tôi thực sự tôi không biết whats tôi có thể làm gì để sắp xếp các biểu tượng. Tôi đã googled giờ và tôi đã thử tất cả senario

những gì là sai của tôi? cho tất cả các thử nghiệm tôi không thể thấy bất kỳ thay đổi nào ... enter image description here

trước khi tôi muốn pic2 (ở trên ảnh) Nhưng bây giờ tôi chỉ muốn căn giữa nó!

Đối với tất cả các thử nghiệm của tôi, tôi chỉ nhận được hình ảnh 1. không có bất kỳ thay đổi nào.

+0

Trên thực tế bạn không sắp xếp tất cả các mục trong trung tâm. một là trái và một là căn phải và 4 là căn giữa. Vì vậy, đề xuất của tôi thay vì sử dụng menu bên trong 'Thanh công cụ' sử dụng' RelativeLayout'. –

+0

tại sao không sử dụng bố cục tùy chỉnh thay vì thanh công cụ? –

Trả lời

2

Thanh công cụ giống như bất kỳ chế độ xem nào khác. Bạn có thể thêm trẻ em vào nó trực tiếp và truy cập sau đó như bạn sẽ cho một cái nhìn bình thường.

Đây không phải là câu trả lời chính xác cho câu hỏi của bạn, nhưng nó sẽ cho bạn biết cách đi.

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:background="@android:color/holo_red_light" 
    android:elevation="2dp" 
    android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/ic_launcher" 
      android:id="@+id/button1"/> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/ic_launcher" 
      android:id="@+id/button2"/> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/ic_launcher" 
      android:id="@+id/button3"/> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/ic_launcher" 
      android:id="@+id/button4" /> 

    </LinearLayout> 

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

Sau đó, trong đoạn chính của bạn hoặc nơi nào khác bạn có thể truy cập vào một cái gì đó như thế này.

Toolbar mToolbar = (Toolbar) root.findViewById(R.id.tool_bar); 

    Button button1 = (Button) mToolbar.findViewById(R.id.button1); 
    Button button2 = (Button) mToolbar.findViewById(R.id.button2); 
    Button button3 = (Button) mToolbar.findViewById(R.id.button3); 
    Button button4 = (Button) mToolbar.findViewById(R.id.button4); 

Bạn có thể sử dụng phương pháp này để tạo thanh công cụ tùy chỉnh theo bất kỳ cách nào bạn muốn. Có thể bạn sẽ muốn sử dụng bố cục tương đối.

0

Bạn nên cố gắng áp dụng bất kỳ căn chỉnh nào cho bố cục bố mẹ.

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"> 
<Button ...any attributes.../> 
...any items... 
</LinerLayout> 
+0

Tôi đã làm nó cho tất cả các bậc cha mẹ ... không có thay đổi ... Tất cả các biểu tượng nổi bên trái. – partiz

+0

Có thể nó xung đột với một liên kết con hoặc cha mẹ khác. Cung cấp cho chúng tôi bố cục của bạn xml – Kaloglu

0

Tôi biết, giải pháp này là khủng khiếp, nhưng đối với tôi nó hoạt động.

Thứ nhất, trong LinearLayout bạn có thể đặt 3 Toolbars thay vì 1:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <android.support.v7.widget.Toolbar android:id="@+id/bottomToolbarLeft" 
     android:layout_width="0dp" 
     android:layout_height="?attr/actionBarSize" 
     android:layout_weight="1.0" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay"/> 

    <android.support.v7.widget.Toolbar android:id="@+id/bottomToolbarCenter" 
     android:layout_width="wrap_content" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay"/> 

    <android.support.v7.widget.Toolbar android:id="@+id/bottomToolbarRight" 
     android:layout_width="0dp" 
     android:layout_height="?attr/actionBarSize" 
     android:layout_weight="1.0" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay"/> 

</LinearLayout> 

Mỗi thanh công cụ nên có nguồn tài nguyên thực đơn riêng nó `s.

Sau đó, bạn sẽ nhận được một cái gì đó như thế:

Cuối cùng, để sắp xếp biểu tượng trái, bạn có thể dễ dàng thiết lập thông số này cho thanh công cụ bên trái:

android:layoutDirection="rtl" 

Kết quả là: The desired order

5

Khắc phục sự cố, tận hưởng :)

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:elevation="4dp" 
    android:background="?attr/colorPrimary"> 
    <!-- Code for your Left Button --> 
    <ImageView 
     android:id="@+id/yourId" 
     android:src="@mipmap/yourLeftIcon" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:layout_marginTop="4dp" 
     android:layout_marginBottom="4dp" 
     android:layout_gravity="left" /> 
     <!-- Here is the main code for your Middle Buttons --> 
     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:orientation="horizontal" 
      android:layout_gravity="center" 
      android:gravity="center"> 
      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/ic_launcher" 
       android:id="@+id/button1"/> 
      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/ic_launcher" 
       android:id="@+id/button2"/> 
      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/ic_launcher" 
       android:id="@+id/button3"/> 
      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/ic_launcher" 
       android:id="@+id/button4" /> 
     </LinearLayout> 
    <!-- Code for your Right Button --> 
    <ImageView 
     android:id="@+id/yourId" 
     android:src="@mipmap/yourRightIcon" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:layout_marginTop="4dp" 
     android:layout_marginBottom="4dp" 
     android:layout_gravity="right" /> 
</android.support.v7.widget.Toolbar> 
+0

Tôi nhận được kết quả tốt hơn bằng cách (a) lấy tất cả các lề (b) có chiều cao và chiều rộng biểu tượng cố định và (c) thêm 'android: layout_weight =" 1 "' vào 'LinearLayout' cho các nút giữa sao cho nó lấp đầy không gian giữa còn lại và trung tâm tốt hơn. – SharpC

0

bạn có thể làm điều đó theo chương trình như sau:

mToolbar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      ImageView imageView = (ImageView)mToolbar.findViewById(R.id.logo); 
      if(mToolbar == null){ 
       return; 
      } 
      int toolbarWidth = mToolbar.getWidth(); 
      int imageWidth = imageView.getWidth(); 
      imageView.setX((toolbarWidth-imageWidth)/2); 
     } 
    }); 

tùy chỉnh layout_toolbar.xml của bạn:

<android.support.v7.widget.Toolbar 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      android:id="@+id/toolbar" 
      style="@style/Toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/toolbar_height"                 
      android:background="@drawable/toolbar_background" 
      android:focusable="false"         
      app:titleTextAppearance="@style/AppTheme.Toolbar.Title"> 
      <ImageView 
       android:id="@+id/logo" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/toolbar_logo"/> 

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

lẽ giúp bạn:

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

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appBar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbarPath" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary"> 

      <ImageView 
       android:id="@+id/imgBack" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_gravity="left" 
       android:layout_marginBottom="4dp" 
       android:layout_marginLeft="20dp" 
       android:layout_marginRight="20dp" 
       android:layout_marginTop="4dp" 
       android:src="@drawable/ic_action_back" /> 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:layout_gravity="center" 
       android:gravity="center" 
       android:orientation="horizontal"> 

       <ImageView 
        android:id="@+id/imgClear" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginRight="15dp" 
        android:src="@drawable/ic_action_discard" /> 

       <ImageView 
        android:id="@+id/imgStop" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginRight="15dp" 
        android:src="@drawable/ic_action_stop" /> 

       <ImageView 
        android:id="@+id/imgPath" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginRight="15dp" 
        android:src="@drawable/ic_action_merge" /> 

       <ImageView 
        android:id="@+id/imgToggle" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/ic_action_map" /> 
      </LinearLayout> 

      <ImageView 
       android:id="@+id/imgForward" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_gravity="right" 
       android:layout_marginBottom="4dp" 
       android:layout_marginLeft="20dp" 
       android:layout_marginRight="20dp" 
       android:layout_marginTop="4dp" 
       android:src="@drawable/ic_action_forward" /> 
     </android.support.v7.widget.Toolbar> 
    </android.support.design.widget.AppBarLayout> 


    <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/mapPath" 
     android:name="com.google.android.gms.maps.SupportMapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/appBar" /> 

</RelativeLayout> 

Trong style.xml ghi của bạn:

<style name="AppTheme.NoActionBar"> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
</style> 

Sau đó, trong manifest.xml:

<application 
    android:name="com.ir.zanis.app.AppController" 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme.NoActionBar"> <===See here=== 
     ....... 
     ............. 
     ............. 
Các vấn đề liên quan