【发布时间】:2011-04-20 15:13:14
【问题描述】:
我有一个自定义 TabHost,可以添加这样的标签
private void setTab(View view, String tag, Intent intent)
{
View tabView = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(tag);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabView)
.setContent(intent);
mTabHost.addTab(setContent);
}
其中 mTabHost 是选项卡主机,而 tabs_bg.xml 在线性布局中只有一个文本视图。 (我的主要布局与the Tab Layout example 相同;我只是想使用小的纯文本选项卡。)我的信息选项卡是这样调用的:
intent = new Intent().setClass(this, AboutScreen.class);
setTab(new TextView(this), "about", intent);
AboutScreen 扩展了 Activity,它所做的只是 setContentView 到 this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:id="@+id/AboutUsTitle"
android:textColor="#ffffffff" android:text="@string/about_title"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:layout_gravity="center" android:gravity="center"
android:background="@drawable/about_title"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="15dip">
<TextView android:id="@+id/AboutContents"
android:text="@string/about_contents" android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
</LinearLayout>
@drawable/about_title 在哪里:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#ffffffff"
android:endColor="#ff333333"
android:angle="90"/>
</shape>
该可绘制对象未显示在 FrameLayout 中。其他一切都正确显示。任何想法我做错了什么?
编辑:如果我以编程方式设置它
TextView tvAboutUsTitle = (TextView) findViewById(R.id.AboutUsTitle);
tvAboutUsTitle.setBackgroundResource(R.drawable.about_title);
它出现了。为什么这与在 xml 中设置不同?
【问题讨论】:
-
我看到的不同之处在于您的可绘制对象的名称 - 'about_title' 与 'gradient'。它们是相同的可绘制对象吗?
-
糟糕!是的,这是我在测试使用@color 引用是否有所作为时将内容复制到新文件中的错。我将编辑原件以更正。
标签: android xml layout drawable android-tabhost