【问题标题】:Blue color background TabItem when pressed按下时的蓝色背景 TabItem
【发布时间】:2018-11-29 19:59:52
【问题描述】:

我是初学者 Android 程序员。我创建了一个 TabView,当按下选项卡项目中的任何项目时,颜色应该是灰色的,但它会变成蓝色。以前,我在导航底部菜单中遇到了这个问题。我糊涂了。问题出在哪里?我在最后放了两张截图来解释我想要什么以及发生了什么。

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_above="@id/tabs"
        android:layout_alignParentTop="true" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ثبت نقدی" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="پیامک بانکی" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="نمودار" />

        <android.support.design.widget.TabItem
            android:id="@+id/tabItem4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="بیشتر" />

    </android.support.design.widget.TabLayout>

</RelativeLayout>

在下面的图片中,按下 پیامک بانکی 按钮。

这就是我想要的:

这是我的问题。我不知道蓝色是从哪里来的。

【问题讨论】:

    标签: android tabitem


    【解决方案1】:

    我认为您可以创建客户选择器。转到可绘制文件夹并右键单击。然后选择New->create DrawableResource。将文件名添加为 tab_selector。然后将以下代码添加到该文件中

        <?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@color/tab_background_selected" android:state_selected="true"/>
        <item android:drawable="@color/tab_background_unselected"/>
    </selector>
    

    转到值->颜色文件并添加这两种颜色

    <color name="tab_background_selected">#991c47</color>
    <color name="tab_background_unselected">#4d8e63</color>
    

    您可以根据自己的喜好自定义这些颜色。

    然后将 app:tabBackground="@drawable/tab_selector" 添加到选项卡布局视图中

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabBackground="@drawable/tab_selector"
        android:layout_alignParentBottom="true">
    

    【讨论】:

    • 这不是我想要的 当你按下项目时,颜色是蓝色的,而不是当它被选中时。无论如何,如果我没有得到好的结果这个解决方案可以提供帮助。
    • 感谢回答!我删除了 并将 state_selected 更改为 state_Pressed!。
    【解决方案2】:

    这可能是因为你的accentColor 是蓝色的,并且你没有在你的xml 中设置任何其他颜色,所以android 正在使用默认值。

    试试这个来设置你的 ViewPager 和 TabLayout

    // Get the ViewPager and set it's PagerAdapter so that it can display items
    ViewPager viewPager = (ViewPager) findViewById(R.id.container);
    PageAdapter pageAdapter = new PageAdapter(getSupportFragmentManager(), this);
    viewPager.setAdapter(pageAdapter);
    
    // Give the TabLayout the ViewPager
    final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    

    obs:PageAdapter 必须是您的 ViewPager 适配器

    然后在你的xml中

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/main_content"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:fitsSystemWindows="true"
     tools:context=".Activity_Main">
    
    
     <android.support.v4.view.ViewPager
         android:id="@+id/container"
         android:layout_width="match_parent"
         android:layout_height="0dp"
         android:layout_above="@id/tabs"
         android:layout_alignParentTop="true" />
    
     <android.support.design.widget.TabLayout
         android:id="@+id/tabs"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_alignParentBottom="true"
         android:tabBackground="@color/your_color"
         app:tabIndicatorColor="@color/your_color"/>
    
     </RelativeLayout>
    

    【讨论】:

    • 我试过这段代码,但它只改变了背景和下面的行我第一次进入程序时,它工作正常,但下次它又变蓝了 或者,有时,按下按钮快速转动又蓝了
    • 我认为更改应该发生在 TabItem 而不是 TabLayout 上。
    • @Mehran 这正是我写的。这两个参数应该在 TabLayout 中,您还可以从托管该布局的活动中发布一些代码吗?
    • 对不起,我想我误解了。我在 TabLayout 上做了这段代码,我认为 TabItem 应该改变。我有一点英语问题。你想用 MainActivity 代码做什么?
    • 我想知道你是如何设置你的 viewpager 和 tabLayout 的,因为你的 xml 中不需要那个 TabItem。
    猜你喜欢
    • 2014-04-14
    • 2011-10-09
    • 2022-01-21
    • 1970-01-01
    • 2013-10-15
    • 1970-01-01
    • 2021-08-18
    • 2023-04-01
    • 2016-06-08
    相关资源
    最近更新 更多