android:将Tab的单击颜色设置为TableRow的背景颜色

发布于 2024-11-08 11:17:29 字数 521 浏览 0 评论 0原文

我有一个选项卡布局。我有一个带有动态添加行的表格布局。 当选择/单击 TableRow 时,我想将其背景颜色设置为选项卡的按下或单击颜色。

我不想设置任何静态颜色,但想使用默认主题颜色。如何获取 Tablayout 的颜色信息?

如何处理选项卡的左侧和右侧?这就是我在将 tab_background.xml 设置为所有选项卡的 backgroundRsourse 之后得到的结果: Tab bg settings

左侧,两张图片都没有任何设置,但左右角都是平滑的。 在右侧,两个图像都带有设置,请看到所选选项卡的角只是一个正方形,它们应该像没有任何设置的选项卡一样被圆化或平滑。 看来我必须使用 tab_selected_bar_right 和 tab_selected_bar_left 。我已将它们复制到我的绘图中,但不知道如何使用它们。对 @mudit 提供的下面的 xml 进行哪些设置。

请指导我。非常感谢任何帮助。我很急。

谢谢

I have a TabLayout. In that I have a tablelayout with dynamically added rows.
When a TableRow is selected/clicked I want to set its background color to the Pressing or Clicked color of Tab.

I don't want to set any static color but want to use the default theme color. How to get the color info of Tablayout ?

How to handle the left and right side of the tabs ? This is what I get begore and after setting the tab_background.xml as th backgroundRsourse for all tabs :
Tab bg settings

On the left hand side, both the images are without any settings, but the right and left corners are smooth.
On right hand side, both images are with setings, see the corners of selected tabs are just a square, they should be rounded off or smoth like the ones without any settings.
It seems I got to use tab_selected_bar_right and tab_selected_bar_left. I have copied them to my drawable, but can't make out how do I use them. What settings to give int the xml below provided by @mudit.

Please guide me. Any help is highly appreciated. I am in a hurry.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七秒鱼° 2024-11-15 11:17:29

在您的java文件中添加选项卡后添加此代码:(

我只添加2个选项卡,如果您有更多选项卡,您可以添加此代码)

TabWidget tw = getTabWidget();

View tab1View = tw.getChildAt(0);
tab1View.setBackgroundResource(R.drawable.tab_background);

View tab2View = tw.getChildAt(1);
tab2View.setBackgroundResource(R.drawable.tab_background);

代码tab_background.xml:(

将其放入drawable文件夹中)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_focused="false" android:state_selected="false"
        android:state_pressed="false"
        android:drawable="@drawable/tab_bg" />

    <item android:state_focused="false" android:state_selected="true"
        android:state_pressed="false" android:drawable="@drawable/tab_selected" />

    <item android:state_focused="true" android:state_selected="false"
        android:state_pressed="false"
        android:drawable="@drawable/tab_bg" />

    <item android:state_focused="true" android:state_selected="true"
        android:state_pressed="false" android:drawable="@drawable/tab_selected" />

    <item android:state_pressed="true"
        android:drawable="@drawable/tab_selected" />
</selector> 

Add this code after adding the tabs in your java file:

( I am adding only 2 tabs, you can add this if you have more tabs)

TabWidget tw = getTabWidget();

View tab1View = tw.getChildAt(0);
tab1View.setBackgroundResource(R.drawable.tab_background);

View tab2View = tw.getChildAt(1);
tab2View.setBackgroundResource(R.drawable.tab_background);

Code tab_background.xml:

(put this in drawable folder)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_focused="false" android:state_selected="false"
        android:state_pressed="false"
        android:drawable="@drawable/tab_bg" />

    <item android:state_focused="false" android:state_selected="true"
        android:state_pressed="false" android:drawable="@drawable/tab_selected" />

    <item android:state_focused="true" android:state_selected="false"
        android:state_pressed="false"
        android:drawable="@drawable/tab_bg" />

    <item android:state_focused="true" android:state_selected="true"
        android:state_pressed="false" android:drawable="@drawable/tab_selected" />

    <item android:state_pressed="true"
        android:drawable="@drawable/tab_selected" />
</selector> 
对你再特殊 2024-11-15 11:17:29

我解决了这个问题。我很抱歉没有早点提到这一点。抱歉,我刚刚错过了。
@Yugandhar,感谢您的关注。

Am solved with this issue. I apology for not mentioning it earlier. Sorry, I just missed out.
@Yugandhar, Thanks for your interest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文