2017-10-15 316 views
1

图标和标题不会出现在AppCombat工具栏。图标和标题不会出现在工具栏AppCombat

toolbar at the emulator

这是菜单我的xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<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=".HolidayListActivity"> 

<item android:id="@+id/action_settings" 
android:title="@string/app_name" 
android:orderInCategory="100" 
app:showAsAction="always" /> 
</menu> 

这是我的资源的开发文件:

<resources> 
<style name="Theme.AppCompat.Light.FullScreen" 
parent="Theme.AppCompat.Light.DarkActionBar"> 

<item name="android:windowBackground">@color/window_background</item> 
<item name="android:colorBackground">@color/window_background</item> 
<item name="android:colorBackgroundCacheHint">@color/window_background</item> 
<item name="windowNoTitle">true</item> 
<item name="windowActionBar">false</item> 
<item name="android:windowFullscreen">true</item> 
<item name="android:windowContentOverlay">@null</item> 
</style> 

</resources> 

这是我的Java代码:

public class HolidayListActivity extends SingleFragmentActivity { 

    private Toolbar toolbar; 

ActionBarDrawerToggle mDrawerToggle; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

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

    getSupportActionBar().setDisplayShowHomeEnabled(true); 
    getSupportActionBar().setLogo(R.mipmap.ic_launcher); 
    getSupportActionBar().setDisplayUseLogoEnabled(true); 

    } 

@Override 
    protected Fragment createFragment() { 
    return new HolidayListFragment(); 
} 

@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_app, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
    return true; 
    } 
    return super.onOptionsItemSelected(item); }} 

这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/holiday_layout" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical"> 

<android.support.v7.widget.Toolbar 
android:id="@+id/toolbar" 
android:minHeight="?attr/actionBarSize" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
app:titleTextColor="@android:color/white" 
android:background="?attr/colorPrimary"> 
</android.support.v7.widget.Toolbar> 

<android.support.v7.widget.RecyclerView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/holiday_recycler_view" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/window_background"> 
</android.support.v7.widget.RecyclerView> 
</LinearLayout> 
+0

为什么使用''windowActionBar“> false'? –

+0

可能要使用工具栏,是吗? – Mehmed

+0

作为第二个问题,SingleFragmentActivity延伸到哪个类? – Mehmed

回答

1

如果您使用的工具栏,你可以描述你的布局文件自己及其组件。

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

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
      <ImageView 
       android:id="@+id/toolbar_icon" 
       android:layout_width="42dp" 
       android:layout_height="42dp" 
       android:layout_centerVertical="true" 
       android:contentDescription="toolbat icon"/> 
      <TextView 
       android:id="@+id/toolbar_title" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       style="@style/TextAppearance.Widget.AppCompat.Toolbar.Title" 
       android:textSize="18sp" 
       android:text="toolbar title" 
       android:maxLines="1" 
       android:layout_toEndOf="@id/toolbat_icon" 
       /> 
     </RelativeLayout> 

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