我只想在标签栏中放置文本,没有图像...我想在标签栏中水平和垂直居中放置文本。正好在中心。
最佳答案
如果有人仍然对不创建自己的布局和样式的简单解决方案感兴趣:
- 为每个添加的标签使用 android.widget.TabHost.TabSpec#setIndicator(CharSequence label)
使用下一个代码在选项卡内居中和换行文本:
int tabCount = tabHost.getTabWidget().getTabCount(); for (int i = 0; i < tabCount; i++) { final View view = tabHost.getTabWidget().getChildTabViewAt(i); if ( view != null ) { // reduce height of the tab view.getLayoutParams().height *= 0.66; // get title text view final View textView = view.findViewById(android.R.id.title); if ( textView instanceof TextView ) { // just in case check the type // center text ((TextView) textView).setGravity(Gravity.CENTER); // wrap text ((TextView) textView).setSingleLine(false); // explicitly set layout parameters textView.getLayoutParams().height = ViewGroup.LayoutParams.FILL_PARENT; textView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT; } } }
关于android - 如何在 Android 的标签栏中居中对齐文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5164443/