Android:使用 TabHost 和 TabHost 自定义选项卡的外观选项卡小部件
我之前打开过一篇关于此问题的帖子,但我觉得我现在可以(在阅读其他一些帖子之后)更好地解释我想要的内容并重新措辞,以便更好地理解。
我按照开发指南上有关选项卡布局的教程进行操作,并设法用它创建选项卡,但我想对其进行一些自定义(我确实查看了其他帖子,但代码要么有很多错误,要么没有不回答我正在寻找的东西)。
我遇到的第一个问题是测试大部分是在图标上方而不是在图标下方(我按照开发指南中的建议使用了尺寸为 48x48 的图标)。我希望选项卡的行为就像wrap_content 一样。 我还想更改文本大小(我认为它称为标签)。
我想使用十六进制三元组来更改选项卡的背景颜色,以在以下情况之间更改它:何时选择此选项卡以及何时不选择该选项卡。
我希望能够更改选项卡下方线条的颜色,我找不到有关如何执行此操作的任何信息。
我当前用于创建新选项卡的代码是(来自开发指南):(
intent = new Intent().setClass(this, GroupsActivity.class);
spec = tabHost.newTabSpec("groups").setIndicator("groups",
res.getDrawable(R.drawable.ic_tab_groups))
.setContent(intent);
tabHost.addTab(spec);
groups 是选项卡名称)。
非常感谢帮助!
I opend a post about this before but I feel that I can now (after reading some other posts) better explain what I want and rephrase it so it will be better understand.
I followed the tutorial about Tab Layout on the dev guide and I managed to create tabs with it, but I want to do some customization to it (and I did look on other posts, but either the code had many mistakes to it or it didn't answer what I'm looking for).
The first problem I have is that the test is in most part over the icon instead of below it (I used an icon with dimensions 48x48 as recommended on the dev guide). I want the tab with to act like wrap_content does.
I also want to change the text size (I think it's called the label).I want to use hex triplets to change the background color of the tabs, to change it between to situations : when this tab is the one selected and when it's not.
I want to be able to change the color of the line that is below the tabs, I could not find any information on how to do this.
The code I'm currently using to create a new tab is (from the dev guide):
intent = new Intent().setClass(this, GroupsActivity.class);
spec = tabHost.newTabSpec("groups").setIndicator("groups",
res.getDrawable(R.drawable.ic_tab_groups))
.setContent(intent);
tabHost.addTab(spec);
(groups is the tab name).
Help is very much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与其尝试自定义小部件选项卡本身,这里是我在一个项目中成功使用的另一种方法,它可能会为您省去一些麻烦:
这个想法是在布局中使用隐藏的 TabWidget 并使用自定义的 LinearLayout 来控制它,其中包含按钮。这样,您可以更轻松地将按钮自定义为您想要的外观。您将在每个按钮的 OnClick 内控制 Activity 中的实际 TabWidget。
使用 TabWidget 和按钮创建布局:
设置 Activity 的 onCreate 来处理使用按钮调整选项卡视图的情况:
如您所见,我使用可绘制对象来显示按钮打开和关闭的图像。使用此技术,当您只是尝试自定义 TabWidget 选项卡的外观时,您将不受可用选项的限制,并且您可以为选项卡创建完全自定义的外观。
Rather than trying to customize the widget tabs themselves, here is an alternate approach that I've used successfully on a project that may save you some headaches:
The idea is to use a hidden TabWidget in your layout and control it with a customized LinearLayout containing Buttons. This way, you can more easily customize the buttons to look however you'd like. You'll control the actual TabWidget in your Activity within each button's OnClick.
Create your layout with both the TabWidget and the Buttons:
Set up the onCreate of your activity to handle using the buttons for adjusting the tab views:
As you can see, I'm using drawables for the images of the buttons on and off. Using this technique, you're not limited to the options available when simply just trying to customize the look of the TabWidget's tabs and you can create a completely custom look to your tabs.
1- 使用自定义视图:
而不是:
然后在文件 tabwidget_tabs.xml 中定义选项卡的视图(您可以在 textView 和 textsize 之前定义 ImageView):
2- 不可能使用十六进制三元组来更改选项卡的背景颜色,因为是可绘制的而不是颜色。但是,您可以使用选择器来更改可绘制对象。您可以将此解决方案与 setColorFilter() 和 android:tint 结合使用,然后您可以使用十六进制三元组选择背景: 如何给位图着色
tabs_bkgrd.xml:
您可以定义颜色或形状,tab_selected_shape.xml:
3-该线是可绘制的 也。您可以在sdk中找到这些文件,然后使用gimp修改它们以更改颜色后将其复制到您的项目中。您可以将此解决方案与 setColorFilter() 和 android:tint 结合使用,然后您也可以使用十六进制三元组选择背景。读:
进一步说明
android-sdk-linux_x86/platforms/android-7/data/res/drawable
android-sdk-linux_x86/platforms/android-7/data/res/drawable-mdpi
1- Use a custom view:
instead of:
And then define the view for the tabs in the file tabwidget_tabs.xml (you can define an ImageView before the textView and the textsize):
2- It's not possible to use hex triplets to change the background color of the tabs because are drawables not colors. However you can use a selector that changes the drawables. and you can combine this solution with setColorFilter() and android:tint and then you can select the background using hex triplets: How to tint a bitmap
tabs_bkgrd.xml:
You can define a color or a shape, tab_selected_shape.xml:
3- The line is a drawable too. you can find the files in the sdk and copy them into your project after modify them to change the color using gimp. You can combine this solution with setColorFilter() and android:tint and then you can select the background using hex triplets too. Read:
further explanation
android-sdk-linux_x86/platforms/android-7/data/res/drawable
android-sdk-linux_x86/platforms/android-7/data/res/drawable-mdpi
我在 这个问题?
您可以使用本机 Android 选项卡栏使用的相同内容自定义每个按钮的可绘制内容(在 Android.jar 中查找资源以找到正确的可绘制内容),此外您还可以根据需要自定义其他行为。
最后,从用户的角度来看,您将获得图形上类似于选项卡栏的东西,但从开发人员的角度来看,其行为却有所不同。
What about the solution I proposed on this question?
You can customize the drawable of each button using the same used by native Android Tab bar(looking for resources in Android.jar to find the right drawables), plus you can customize additional behavious as you desire.
At the end, you will obtain something that is graphically similar to a tabbar, from an user perspective, but acts differently from a developer perspective.