Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android中的定制表格布局_Android_Android Tablayout - Fatal编程技术网

Android中的定制表格布局

Android中的定制表格布局,android,android-tablayout,Android,Android Tablayout,我正在尝试建立一个自定义的TabLayout作为库的一部分,它可以被其他应用程序使用。此TableLayout中的每个选项卡还需要具有自定义背景、属性 public class MyTab extends TabLayout { Context mContext; public MyTab(Context context) { this(context, null); } public MyTab(Cont

我正在尝试建立一个自定义的TabLayout作为库的一部分,它可以被其他应用程序使用。此TableLayout中的每个选项卡还需要具有自定义背景、属性

public class MyTab extends TabLayout {
        Context mContext;
        public MyTab(Context context) {
            this(context, null);
        }

        public MyTab(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }

        public MyTab(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            setBackground(context.getResources().getDrawable(R.drawable.tab_background));
            setTabMode(MODE_SCROLLABLE);
            mContext = context;
            setSelectedTabIndicatorColor(context.getResources().getColor(R.color.lightest_grey9));

        }

        @Override
        public void addTab(@NonNull Tab tab) {
            super.addTab(tab);
            setTabGravity(GRAVITY_FILL);
            View tabView = LayoutInflater.from(mContext).inflate(R.layout.tab_item_background,null);
            TextView textView = (TextView) tabView.findViewById(R.id.tab_text);
            textView.setText(tab.getText());
    //        textView.setFocusable(true);
            tabView.setFocusable(true);
            tabView.setFocusableInTouchMode(true);
    //        textView.setBackground(getContext().getResources().getDrawable(R.drawable.tab_item_view_background));
            tabView.setBackground(getContext().getResources().getDrawable(R.drawable.tab_item_view_background));

            tab.setCustomView(tabView);
        }
    }
我的要求是:

  • 所选指示器应为灰色
  • 当选项卡项处于焦点时(键盘选项卡键导航/左右箭头键导航),文本周围应该有一个矩形框
  • 这种方法的问题:

  • 当我试图设置自定义视图时,我的文本丢失,我需要显式设置文本。现在用户可能需要设置文本/图标/文本+图标(垂直或水平方向)。检测用例并设置合适的背景可能不是很好的实现。有没有更好的方法来实现这一点
  • 在TabLayout内部,两个选项卡之间的焦点只能通过“tab”键导航进行切换。我还希望在Tablayout中使用左/右箭头键导航。有没有办法做到这一点
  • 在TabLayout内部,当我试图将焦点从一个选项卡转移到另一个选项卡时,第一次“tab键导航”完美地转移了焦点(我得到了文本周围的矩形),但这是第二次(当所有选项卡都已用尽且tab键导航将焦点放在第一个选项卡上时,我没有得到它周围的矩形,这意味着我的自定义视图焦点状态drawable没有被应用,第三次它也可以正常工作。有没有解决此替代行为的方法
  • 我错过什么了吗

    tab_item_background.xml
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <TextView
            android:focusableInTouchMode="true"
            android:id="@+id/tab_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/lightest_grey9" android:background="@drawable/tab_item_view_background"/>
    </LinearLayout>
    
    tab_focused.xml
    
    <?xml version="1.0" encoding="utf-8"?>
    <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
            <padding android:left="5dp" android:right="5dp"/>
    
            <size android:height="38dp" android:width="40dp"/>
            <stroke android:width="2dp"
                android:color="@color/lightest_blue5"/>
    </shape>
    
    tab_item_view_background.xml
    
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_focused="true" android:drawable="@drawable/tab_focused">
        </item>
    </selector>
    
    tab\u item\u background.xml
    tab_focused.xml
    选项卡\u项目\u视图\u background.xml
    

    欢迎使用Stack Overflow!我编辑了您的问题以使其更具可读性。有关格式设置的详细信息,请查看。另外,请尽量不要让您的问题过于宽泛,或一次问太多问题,并尝试提供一个解决方案。有时太多的信息或代码实际上可能会使人们更难回答您的问题。祝您好运!谢谢@马修斯拉塞尔达