代码之家  ›  专栏  ›  技术社区  ›  Simon

Android TabLayout带有自定义视图选项卡-图标单击不会切换选项卡

  •  2
  • Simon  · 技术社区  · 8 年前

    tabsLayout ImageView 和a TextView 当我想切换选项卡并单击其中一个时,如果我在图标外单击,它们会正确切换。但是,当我单击图标时,它们不会切换。

    enter image description here

    我尝试使用focusable=false,但对我无效。

    我的自定义视图:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/customButton"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:alpha="0.5"
        android:orientation="vertical">
    
        <ImageButton
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_height="0dp"
            android:layout_weight="4"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:background="@android:color/transparent"
            android:onClick="airspacesButtonOnClick"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:tint="@color/colorIcons"/>
    
        <TextView
            android:id="@+id/string"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:textAlignment="center"
            android:textSize="12sp" />
    </LinearLayout>
    

    我的表格布局:

    <android.support.design.widget.TabLayout
          android:id="@+id/tabs"
          android:layout_width="match_parent"
          android:layout_height="80dp"
          app:tabIndicatorColor="@color/colorButton"
          app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"
          app:tabMode="fixed"
          app:tabGravity="fill"/>
    <android.support.v4.view.ViewPager
          android:id="@+id/viewpager"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    

    int[] iconIds = {R.drawable.ic_profile, R.drawable.ic_plane, R.drawable.ic_document};
    int[] labelIds = {R.string.menu, R.string.menu, R.string.menu};
    View customView;
    for (int i = 0; i < tabLayout.getTabCount(); i++) {
    customView = getActivity().getLayoutInflater().inflate(R.layout.custom_icon, null);
    customView.findViewById(R.id.icon).setBackgroundResource(iconIds[i]);
    
    ((TextView) customView.findViewById(R.id.string)).setText(labelIds[i]);
    tabLayout.getTabAt(i).setCustomView(customView);
    
    1 回复  |  直到 8 年前
        1
  •  3
  •   Mehmed Andrew Lam    8 年前

    您试图通过设置 clickable onClick 覆盖它,您的ImageView将再次可单击。这就是为什么单击ImageView会被ImageView本身使用,而不会传播到其父对象。因此,应删除自定义选项卡布局的ImageView中的这一行:

    android:onClick="airspacesButtonOnClick"