Android 带有文本和图像的选项卡视图不显示文本
我是android新手。我现在正在测试带有一些文本和图像的选项卡视图。图像显示,但文本不显示。android SDK版本为4.4.3。请帮我找到它。我看了很多其他地方,但都不适合我 代码如下:Android 带有文本和图像的选项卡视图不显示文本,android,android-tabhost,Android,Android Tabhost,我是android新手。我现在正在测试带有一些文本和图像的选项卡视图。图像显示,但文本不显示。android SDK版本为4.4.3。请帮我找到它。我看了很多其他地方,但都不适合我 代码如下: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TabHost tab
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost =(TabHost) findViewById(R.id.tabHost);
tabHost.setup();
TabHost.TabSpec spec=tabHost.newTabSpec("mitab1");
spec.setContent(R.id.tab1);
//spec.setIndicator("",getResources().getDrawable(android.R.drawable.ic_menu_rotate));
spec.setIndicator(prepareTabView("ABCD", R.drawable.talk));
TabHost.TabSpec spec1=tabHost.newTabSpec("mitab1");
spec1.setContent(R.id.tab2);
spec1.setIndicator(prepareTabView("CDEF", R.drawable.ball));
tabHost.addTab(spec);
tabHost.addTab(spec1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private View prepareTabView(String text, int resId) {
View view = LayoutInflater.from(this).inflate(R.layout.tabcustom, null);
ImageView iv = (ImageView) view.findViewById(R.id.TabImageView);
TextView tv = (TextView) view.findViewById(R.id.TabTextView);
iv.setImageResource(resId);
tv.setText(text);
return view;
}
这是tabcustom.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/TabLayout"
android:padding="5dip"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/TabImageView"
android:src="@drawable/football"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/TabTextView"
android:text="Text"
android:paddingTop="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
/>
布局的方向是垂直的,这就是文本将显示在选项卡底部的原因&若选项卡背景颜色和文本颜色相同,则更改文本颜色。您可以使用下面的代码来显示带有背景图像的中心文本
更改xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/TabLayout"
android:padding="5dip"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/TabTextView"
android:text="Text"
android:paddingTop="5dip"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:textColor="#ffffff"
/>
你能发布R.layout.tab自定义xml布局文件吗…?请参阅@Gopal Rao=>已编辑:我认为你的TextView文本颜色是透明的。但是很好…谢谢你。我的错。我使背景色和文本色相同。真可惜。在答案区写下你的答案。先生,请回答这个问题
private View prepareTabView(String text, int resId) {
View view = LayoutInflater.from(this).inflate(R.layout.tabcustom, null);
LinearLayout iv = (LinearLayout) view.findViewById(R.id.TabLayout);
TextView tv = (TextView) view.findViewById(R.id.TabTextView);
iv.setImageResource(resId);
tv.setText(text);
return view;}