1

my toolbarКак изменить положение ActionOverflow в панели инструментов

В моем изображении выше, я с помощью панели инструментов в качестве моего действия бара. В этой панели у меня есть TextView, которые содержат имя президента и у меня есть tablayout, которые содержат 3 вкладки (item1, ст.2, ст.3)

когда я раздувать мое меню в onCreateOptionsMenu(Menu menu) метод, мой ActionOverflow (в три белых точки) появляется ниже моего текстового комментария президента, и я хотел бы появиться в крайнем правом тексте президента. это влияет на мою ширину tablayout, потому что она не может соответствовать родительской (панели инструментов), которая заполняет ширину экрана.

Как я могу достичь своей цели?

моя активность XML

<android.support.v7.widget.Toolbar 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="?attr/actionBarSize" 
    android:id="@+id/mainActivityBar" 
    android:layout_alignParentTop="true" 
    android:background="@drawable/actionbar" 
    app:contentInsetLeft="0dp" 
    app:contentInsetStart="0dp" 
    app:contentInsetEnd="0dp" 
    app:contentInsetRight="0dp" 
    app:popupTheme="@style/Theme.AppCompat.Light.NoActionBar"> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     > 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="President" 
      android:id="@+id/appname_1" 
      android:background="@android:color/transparent" 
      android:textColor="#ffffff" 
      android:layout_marginLeft="20dp" /> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      style="@style/myCustomTabLayout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@android:color/transparent" 
      android:layout_below="@+id/appname_1" 
      android:layout_alignParentEnd="true" 
      android:layout_alignParentStart="false" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentEnd="true" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentBottom="false" 
      android:fillViewport="true" 
      android:layout_alignLeft="@+id/appname_1" 
      app:tabGravity="fill" 
      app:tabMode="scrollable"/> 
    </RelativeLayout> 

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

мои syles

<style name="mainActivityTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="windowActionBar">false</item> 
    <item name="android:windowNoTitle">true</item> 
    <item name="android:textColorSecondary">@android:color/white</item> 
    <!--We will be using the toolbar so no need to show ActionBar--> 
    <item name="android:windowActionBar">false</item> 

</style> 

<style name="myCustomTabLayout" parent="Widget.Design.TabLayout"> 
    <item name="tabIndicatorColor">?attr/colorAccent</item> 
    <item name="tabIndicatorHeight">2dp</item> 
    <item name="tabSelectedTextColor">?android:textColorPrimary</item> 
</style> 

ответ

0

Используйте этот код. В моем случае это работает правильно.

?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 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" 
    android:id="@+id/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context=".MainActivity"> 


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

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/AppTheme.PopupOverlay"> 

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

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end|bottom" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/ic_dialog_email" /> 

</android.support.design.widget.CoordinatorLayout> 

Добавить это в Gradle файле

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:appcompat-v7:23.1.0' 
    compile 'com.android.support:design:23.1.0' 
} 

V21/стили

<resources>> 
    <style name="AppTheme.NoActionBar"> 
     <item name="windowActionBar">false</item> 
     <item name="windowNoTitle">true</item> 
     <item name="android:windowDrawsSystemBarBackgrounds">true</item> 
     <item name="android:statusBarColor">@android:color/transparent</item> 
    </style> 
</resources> 

стили

<!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
    </style> 
    <style name="AppTheme.NoActionBar"> 
     <item name="windowActionBar">false</item> 
     <item name="windowNoTitle">true</item> 
    </style> 
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> 
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> 

</resources> 

Dimens

<resources> 
    <!-- Default screen margins, per the Android Design guidelines. --> 
    <dimen name="activity_horizontal_margin">16dp</dimen> 
    <dimen name="activity_vertical_margin">16dp</dimen> 
    <dimen name="fab_margin">16dp</dimen> 
    <dimen name="appbar_padding_top">8dp</dimen> 
</resources> 

Добавьте к этому ваш Manifest

android:theme="@style/AppTheme" 
+0

как будет я затем использовать FloatingActionButton в моей деятельности? я изменю способ, которым я вызываю панель инструментов и tablayout, так как они теперь находятся в AppBarLayout? –

+0

Удалите FloatingActionButton, если вам это не нужно. Панель инструментов панели инструментов (панель инструментов) findViewById (R.id.toolbar); setSupportActionBar (панель инструментов); TabLayout tabLayout = (TabLayout) findViewById (R.id.tabs); tabLayout.setupWithViewPager (mViewPager); – Sunny

+0

сталкиваются с ошибками в стилях. ** не может решить стиль ** @ style/AppTheme.PopupOverlay –