滑动效果在滑动标签布局中不起作用

时间:2015-10-09 09:23:46

标签: android xml android-layout

我试图在单击滑动选项卡布局中的选项卡时获得涟漪效果,但它无法正常工作。

custom_tab_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp"
>

<ImageView
    android:id="@+id/tabImage"
    android:layout_width="match_parent"
    android:background="@drawable/tab_bg"
    android:layout_height="wrap_content"
    android:layout_gravity="center" />

<TextView
    android:id="@+id/tabText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="bottom|center" />

下面是可绘制的xml文件以获得涟漪效应

V22 / tab_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:attr/colorControlHighlight">
<item
    android:id="@+id/mask"
    android:drawable="@color/white"></item>
</ripple>

下面是MainActivity,我已经为滑动标签实现了背景和指示器的颜色。

MainActivty.java

  mTabs.setCustomTabView(R.layout.custom_tab_layout, R.id.tabText);
    mTabs.setDistributeEvenly(true);

    mTabs.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
    mTabs.setSelectedIndicatorColors(getResources().getColor(R.color.accentColor));
    mTabs.setViewPager(mPager);

感谢任何帮助.. !!

3 个答案:

答案 0 :(得分:3)

在SlidingTabLayout.java中 - &gt; createDefaultTabView() 你会发现

TypedValue outValue = new TypedValue();

只需将此背景添加为 selectableItemBackground

getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);

供参考,请参阅此SlidingTabLayout (Gist)

修改:

很好,现在我得到了你的问题.. 将 custom_tab_layout.xml 背景设置为 selectableItemBackground

android:background="?attr/selectableItemBackground"

答案 1 :(得分:0)

我找到了一个用于在滑动标签中获得涟漪效果的库。让我在这里发布两个文件。

  1. 在Android Studio中,在build.gradle中添加依赖项(编译&#39; it.neokree:MaterialTabs:0.11&#39;)

    请参阅此https://github.com/neokree/MaterialTabs

  2. activity_tab_library.xml

    <?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    
    tools:context="com.example.ramz.material_udemy_slidnerd.TabLibrary">
    
    <include
    android:id="@+id/app_bar"
    layout="@layout/app_bar" />
    
    <!-- for Text Tabs -->
    <it.neokree.materialtabs.MaterialTabHost
    android:id="@+id/materialTabHost"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    
    app:accentColor="#FF9800"
    app:primaryColor="#673AB7"
    app:textColor="#FFFFFF" />
    
    <android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    
    android:layout_height="0dp"
    android:layout_weight="1"/>
    
    </LinearLayout>
    

    3.TabLibrary.java

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentStatePagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.Toast;
    
    import it.neokree.materialtabs.MaterialTab;
    import it.neokree.materialtabs.MaterialTabHost;
    import it.neokree.materialtabs.MaterialTabListener;
    
    
    public class TabLibrary extends AppCompatActivity implements MaterialTabListener {
    
    private MaterialTabHost tabHost;
    private ViewPager viewPager;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tab_library);
    Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
    tabHost = (MaterialTabHost) findViewById(R.id.materialTabHost);
    viewPager = (ViewPager) findViewById(R.id.viewPager);
    
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(adapter);
    //  viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
    viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int posiiton) {
            tabHost.setSelectedNavigationItem(posiiton);
        }
    });
    for (int i = 0; i < adapter.getCount(); i++) {
        tabHost.addTab(tabHost.newTab().setText(adapter.getPageTitle(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_activity_using_library, 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 tab) {
    
    }
    
    @Override
    public void onTabUnselected(MaterialTab tab) {
    
    }
    
    public class ViewPagerAdapter extends FragmentStatePagerAdapter {
    
    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    
    @Override
    public Fragment getItem(int num) {
        return MainActivity.Myfragment.getInstance(num);
    }
    
    @Override
    public int getCount() {
        return 3;
    }
    
    @Override
    public CharSequence getPageTitle(int position) {
        return getResources().getStringArray(R.array.tabs)[position];
    
    }
    
    }
    
    }
    

    这就是全部,我们可以在这里添加任意数量的标签。谢谢

答案 2 :(得分:0)

尝试使用Android设计支持中的TabLayout

compile 'com.android.support:design:23.1.0'

布局:

xmlns:app="http://schemas.android.com/apk/res-auto"
...
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:tabBackground="?attr/selectableItemBackground"/>

另请参阅使用自定义布局的教程 https://guides.codepath.com/android/google-play-style-tabs-using-tablayout