2015-03-02 78 views
2

我有一个小问题找出我的代码中的“错误”。我不知道它是否有bug。但是,正如您在图片1中看到的那样,固定标签的位置位于窗帘抽屉后面。我无法找到/理解这是为什么。我跟着几个教程,他们似乎没有同样的问题。 我试图谷歌它,但我不能找到类似的问题。任何人有过类似的经历吗?材料设计固定选项卡没有工具栏

在android工作室,设计布局似乎是在点,但是,不编译时。

我使用neokree lib,所以我可以在选择标签时使用图标和涟漪效果。我试图使用谷歌的标签布局link here,但只要我试图删除操作栏并应用图标,发生同样的问题。

谢谢!

activity_main

<?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:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<it.neokree.materialtabs.MaterialTabHost 
    android:id="@+id/materialTabHost" 
    android:layout_width="match_parent" 
    android:layout_height="48dp" 
    app:iconColor="@color/iconColor" 
    app:primaryColor="@color/primaryColor" 
    app:accentColor="@color/accentColor" 
    app:hasIcons="true"/> 

<android.support.v4.view.ViewPager 
    android:id="@+id/viewPager" 
    android:layout_width="match_parent" 
    android:layout_height="0px" 
    android:layout_weight="1"/> 


</LinearLayout> 

styles.xml

<resources> 
    <style name="AppTheme" parent="AppTheme.Base"></style> 

    <style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar"> 

     <item name="colorPrimary">@color/primaryColor</item> 
     <item name="colorPrimaryDark">@color/primaryColorDark</item> 
     <item name="colorAccent">@color/accentColor</item> 
     <item name="colorControlHighlight">@color/colorHighlight</item> 

    </style> 
</resources> 

styles.xml V21

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <!-- Base application theme. --> 

    <!--Using same style as in default style.xml file--> 
    <style name="AppTheme" parent="AppTheme.Base"> 
     <item name="android:colorPrimary">@color/primaryColor</item> 
     <item name="android:colorPrimaryDark">@color/primaryColorDark</item> 
     <item name="android:colorAccent">@color/accentColor</item> 
     <item name="android:windowTranslucentStatus">true</item> 
     <item name="android:colorControlHighlight">@color/colorHighlight</item> 
    </style> 

</resources> 

MainActivity

import android.graphics.drawable.Drawable; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.app.FragmentStatePagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.text.SpannableString; 
import android.text.Spanned; 
import android.text.style.ImageSpan; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

import it.neokree.materialtabs.MaterialTab; 
import it.neokree.materialtabs.MaterialTabHost; 
import it.neokree.materialtabs.MaterialTabListener; 


public class MainActivity extends ActionBarActivity implements MaterialTabListener 
{ 

    private Toolbar toolbar; 
    private ViewPager mPager; 
    private SlidingTabLayout mTabs; 

    private MaterialTabHost tabHost; 
    private ViewPager viewPager; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     tabHost = (MaterialTabHost) findViewById(R.id.materialTabHost); 
     viewPager = (ViewPager) findViewById(R.id.viewPager); 

     ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager()); 
     viewPager.setAdapter(adapter); 
     viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
      @Override 
      public void onPageSelected(int position) { 
       tabHost.setSelectedNavigationItem(position); 

      } 
     }); 
     for (int i = 0; i < adapter.getCount(); i++) { 
      tabHost.addTab(
        tabHost.newTab() 
          .setIcon(adapter.getIcon(i)) 
          .setTabListener(this)); 
     } 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) 
     { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    public void onTabSelected(MaterialTab materialTab) { 
     viewPager.setCurrentItem(materialTab.getPosition()); 
    } 

    @Override 
    public void onTabReselected(MaterialTab materialTab) { 

    } 

    @Override 
    public void onTabUnselected(MaterialTab materialTab) { 

    } 

    class ViewPagerAdapter extends FragmentStatePagerAdapter 
    { 

     int icons[] = {R.drawable.ic_home, 
       R.drawable.ic_graph, 
       R.drawable.ic_bell_mid, 
       R.drawable.ic_settings}; 

     //String[] tabText = getResources().getStringArray(R.array.tabs); 

     public ViewPagerAdapter(FragmentManager fm) 
     { 
      super(fm); 
      //tabText = getResources().getStringArray(R.array.tabs); 
     } 

     @Override 
     public Fragment getItem(int position) 
     { 
      MyFragment myFragment = MyFragment.getInstance(position); 
      return myFragment; 
     } 

     //Attaching an image to a (spannable) string so we can show the image instead of text. 
     @Override 
     public CharSequence getPageTitle(int position){ 
      /*Drawable drawable = getResources().getDrawable(icons[position]); 
      //icon bounds/size 
      drawable.setBounds(0,0,96,96); 
      ImageSpan imageSpan = new ImageSpan(drawable); 
      SpannableString spannableString = new SpannableString(" "); 
      spannableString.setSpan(imageSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 
      return spannableString;*/ 
      return getResources().getStringArray(R.array.tabs)[position]; 
     } 

     @Override 
     public int getCount() 
     { 
      return 4; 
     } 

     private Drawable getIcon(int position) 
     { 
      return getResources().getDrawable(icons[position]); 
     } 
    } 
} 

tabLayout activity_main layout

回答

0

意外得到了一个YouTube教程的制作简单材料的设计选项卡后,我做了一个新的项目,并开始同我在YouTube上找到我的旧的结合。最终,我发现样式文件中的以下代码行是问题的原因。

<item name="android:windowTranslucentStatus">true</item> 

它似乎是我为测试添加的东西,以后忘记删除。

0

尝试添加工具栏有没有动作条主题..

为了做到这一点,你必须做出一个新的XML文件名为tool_bar.xml和下面的代码粘贴到其中:

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

然后您将其添加到您的activity_main.xml文件中:

<include 
    android:id="@+id/tool_bar" 
    layout="@layout/tool_bar" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    />