How To Center Icon in Android Action Bar

前端 未结 3 2066
故里飘歌
故里飘歌 2021-01-04 08:25

I\'m trying to center the icon in the android action bar. I\'m using a custom layout that has a button on the left side, an icon in the center and the menu options on the r

相关标签:
3条回答
  • 2021-01-04 08:41

    The previous answers doesn't work for me (on Android 4.4 devices), because the outer container view groups did not expand to the full with, so the container group and the centered inner logo was already left aligned in the title bar.

    I found a layout that works with the regular action bar menu (on right side) and regardless of enabling the regular action bar logo and title (on left side). This example only centers an additional logo.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
    >
    
        <View android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="1"
        />
    
        <ImageView android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:src="@drawable/my_logo"
                   android:layout_gravity="center_vertical"
                   android:contentDescription="Logo"
            />
    
        <View android:layout_width="0dp"
              android:layout_height="match_parent"
              android:layout_weight="1"
          />
    
    </LinearLayout>
    

    You have to enable the custom view by this

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayShowCustomEnabled(true);
    View cView = getLayoutInflater().inflate(R.layout.actionbar, null);
    actionBar.setCustomView(cView);
    

    but not this method (it disables all other elements in action bar).

    // actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    
    0 讨论(0)
  • 2021-01-04 08:50

    Okay. This should work. Try this(Tested in normal activity):

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ActionBarWrapper"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal">
    
        <ImageButton
            android:id="@+id/slideMenuButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:layout_alignParentLeft="true" />
    
        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:layout_centerVertical="true"
            android:layout_centerInParent="true"  />    
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2021-01-04 08:54

    You can add a custom view to the action bar. Just add a LinearLayout as the custom view and add sub views to the linearlayout. I used this technique to add 4 buttons right in the middle.

    To add a custom view use actionBar.setCustomView(view). Also set the option DISPLAY_SHOW_CUSTOM using actionBar.setDisplayOptions.

    Once you have a custom view in the bar then use normal layout procedures to get the effect you want. One trick you could use would be to have 3 nested linear layouts and assign each a weight. For example 1,4,1 so the center layout gets the most space. Here is an example, of course you can leave out the buttons on the right side layout if you want it to be blank. With this approach you can achieve the exact center.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#F00"
            android:orientation="horizontal" >
    
            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="#0F0"
            android:gravity="center_horizontal"
            android:orientation="horizontal" >
    
            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="#00F"
            android:gravity="right"
            android:orientation="horizontal" >
    
            <Button
                android:id="@+id/button3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />
    
        </LinearLayout>
    </LinearLayout>
    
    0 讨论(0)
提交回复
热议问题