如何删除TabLayout的onTouch焦点

时间:2015-12-14 12:46:19

标签: android androiddesignsupport android-tablayout

当我点击我的标签布局标签时,它会显示我的灰色。

如何在触摸时设置透明度。

这是我的xml代码

   <ViewPager
    android:id="@+id/viewpager"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    />
   <android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0"
    android:layout_marginBottom="-3dp"
    android:layout_alignParentBottom="true"
    app:tabMode="fixed"
    app:tabGravity="fill"
    android:layout_marginLeft="50dp"
    android:layout_marginRight="50dp"
    android:background="@null"
    android:overScrollMode="never" /> 

.java文件

    tabLayout.setFocusableInTouchMode(false); //Not Working
    tabLayout.setFocusable(false); //Not Working

Tab layout effect

5 个答案:

答案 0 :(得分:1)

  

如何删除TabLayout的onTouch焦点

tabLayout.clearFocus();

注意:当View清除焦点时,框架会尝试从顶部开始关注第一个可聚焦视图。因此,如果此View是从顶部开始可以获得焦点的第一个视图,那么将调用与清除焦点相关的所有回调,之后框架将关注此视图。

来源:Android文档

答案 1 :(得分:1)

试试这个:

app:tabBackground="@android:color/transparent"

答案 2 :(得分:0)

我认为这不是可访问性的好习惯,但是您可以在这里找到一种对我有用的方法。

解决方案: 将TabLayout设置为不可聚焦是不够的,您必须递归调用Childs并禁用每个焦点。我写了这种方法来递归地设置焦点。

private void setFocusable(View view, boolean focus) {
    view.setFocusable(focus);
    if(view instanceof ViewGroup) {
        ViewGroup viewGroup = (ViewGroup) view;
        for(int i = 0; i<viewGroup.getChildCount(); i++) {
            setFocusable(viewGroup.getChildAt(i), focus);
        }
    }
}

使用示例:

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View layout = inflater.inflate(getLayoutId(), container, false);

    viewPager = layout.findViewById(R.id.viewPager);

    pagerAdapter = getPagerAdapter();
    viewPager.setAdapter(pagerAdapter);
    viewPager.requestFocus();

    tabLayout = layout.findViewById(R.id.tabLayout);
    tabLayout.setupWithViewPager(viewPager, true);

    // disabling focus on TabLayout
    setFocusable(tabLayout, false);

    return layout;
}

答案 3 :(得分:0)

一种衬管解决方案:)。要抑制对Tablayout子视图的关注,请使用以下代码片段:

tabLayout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);

答案 4 :(得分:0)

app:tabRippleColor =“ @ android:color / transparent”

相关问题