2016-09-20 23 views
0

我學習Android的現在,相當newbe這裏, 和困惑工具欄上,被別人閱讀的一切,但似乎並沒有爲我工作。 在MainActivity工具欄做工精細,工具欄上的按鈕轉到正確的沒有填充 但是當我移動到其他活動 按鈕並沒有放置在正確的作爲,如果有一個填充有,機器人 - 工具欄插件沒有充分寬

這裏我toolbar.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.AppBarLayout 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:fitsSystemWindows="true" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
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="#5f0e92" 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 
</android.support.design.widget.AppBarLayout> 

我的第二個活動

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/Lay" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true"> 
<include layout="@layout/toolbar" 
    android:id="@+id/include" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    /> 
<ScrollView 
    android:id="@+id/scrollOff" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_horizontal" 
    android:layout_above="@+id/TotShow" 
    android:layout_below="@+id/include" 
    android:visibility="gone"> 
    <TableLayout 
     android:layout_width="match_parent" android:layout_height="32dp" 
     android:gravity="center_horizontal" 
     android:id="@+id/tableLayoutOff" 
     android:stretchColumns="*"> 
    </TableLayout> 
</ScrollView> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceSmall" 
    android:text="Small Text" 
    android:id="@+id/TotShow" 
    android:layout_gravity="center_horizontal" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" /> 
</RelativeLayout> 

的onCreate功能,我把這個

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 

------------ ----------------編輯

忘記按鈕/肘 toggle.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" android:layout_height="wrap_content" 
android:gravity="right"> 
<Button 
    style="?android:attr/buttonStyleSmall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/button" 
    android:background="@android:drawable/ic_menu_help" 
    android:layout_alignParentTop="true" 
    android:layout_toLeftOf="@+id/actionbar_service_toggle" 
    android:layout_toStartOf="@+id/actionbar_service_toggle" /> 
<ToggleButton 
    android:id="@+id/actionbar_service_toggle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textOff="inchi" 
    android:textOn="milli" 
    android:gravity="right" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" /> 

    </RelativeLayout> 

customview:

ActionBar actionBar = getSupportActionBar(); 
    actionBar.setCustomView(R.layout.togle); 
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM); 
在我的MainActivity

,這項工作完全正常

first mainactivity

在第二活動

似乎有是正確的填充

second activity

+0

您可以發佈與您的按鈕什麼?代碼如何將其添加到工具欄? –

回答

0

解決這個 我很抱歉的麻煩, 似乎我已經做了我的代碼一些愚蠢的事, 我不小心保留這個代碼:

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present.. 

    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

好,我目前瞭解這個Android,所以我只是複製粘貼的主要活動的代碼,禁用在mainactivity中添加菜單後,我忘記在第二個活動中禁用它,所以右側有一個菜單,當我嘗試點擊它時,它是透明的,按鈕工作,這是我意識到我的錯誤的地方。