Android 为什么选项卡视图中的可绘制视图不可见?
我有一个自定义TabHost,它可以添加这样的选项卡Android 为什么选项卡视图中的可绘制视图不可见?,android,xml,layout,drawable,android-tabhost,Android,Xml,Layout,Drawable,Android Tabhost,我有一个自定义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); Ta
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在linearlayout中只有一个文本视图。我的主要布局与相同;我只是想有一个小的,只有文本的标签。“我的信息”选项卡的调用方式如下:
intent = new Intent().setClass(this, AboutScreen.class);
setTab(new TextView(this), "about", intent);
AboutScreen扩展了活动,它所做的只是将ContentView设置为
<?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>
它出现了。为什么这与在xml中设置它不同?我看不到您在任何地方使用tv TextView,除非这段代码中缺少它 下面是我如何在活动中设置3个选项卡的示例,这可能会对您有所帮助 编辑:我忘了提。在这段代码中,我还需要我认为您需要的,一行的自定义选项卡
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this.getApplicationContext();
application = ((rbApplication)getApplicationContext());
setContentView(R.layout.channellist);
new DownloadTask().execute();
setupTabs();
}
public void setupTabs() {
TabHost tabHost = getTabHost(); // The activity TabHost
tabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
TabHost.TabSpec spec; // Reusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Initialize a TabSpec for each tab and add it to the TabHost
intent = new Intent().setClass(application, ChannelListSubscribed.class);
spec = tabHost.newTabSpec("subscribed");
View customTabView1 = createTabView(application, "Subscribed");
spec.setIndicator(customTabView1);
spec.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(application, ChannelListNonSubscribed.class);
spec = tabHost.newTabSpec("nonsubscribed");
View customTabView2 = createTabView(application, "Find More");
spec.setIndicator(customTabView2);
spec.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(application,ChannelListFeatured.class);
spec = tabHost.newTabSpec("featured");
View customTabView3 = createTabView(application, "Featured");
spec.setIndicator(customTabView3);
spec.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
private static View createTabView(final Context context, final String tabLabel) {
View view = LayoutInflater.from(context).inflate(R.layout.tab_item, null);
TextView tv = (TextView) view.findViewById(R.id.tab_label);
tv.setText(tabLabel);
return view;
}
tab_item.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:padding="10dip"
android:gravity="center"
android:orientation="vertical"
android:background="@drawable/tab_bg_selector">
<TextView
android:id="@+id/tab_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textColor="@drawable/tab_text_selector"/>
</LinearLayout>
我遇到过这样的问题:视图显示得不够大,因为它的大小似乎是在它的内容被知道之前确定的,即使它都是用xml保存的,并且引用了字符串 您是否尝试过将布局的高度和宽度设置为数值,甚至“填充父对象”,以查看是否存在这种情况
另外,我会简化布局以删除嵌套的LinearLayouts?因为它们看起来不必要,因为每个都只持有一个文本视图,因为我们现在不使用选项卡视图,所以我要关闭它,因为
TextView tvAboutUsTitle = (TextView) findViewById(R.id.AboutUsTitle);
tvAboutUsTitle.setBackgroundResource(R.drawable.about_title);
修复了我遇到的问题,即使我真的不知道为什么。I do tv.setTexttag;在获取textview后立即。My tabs_bg.xml与您的tab_item.xml相同My@+id/tabsText是您的@+id/tab_标签。如果我调用AboutScreen作为主要活动而不是选项卡布局,则会显示渐变。如果我在选项卡布局内调用它,则不会。我尝试将布局的高度/宽度更改为数值,包括dp和px,但没有任何区别-如果直接查看活动,则会显示可绘制的,但在加载到选项卡时不会显示,除非您以编程方式专门设置它。如果我使用或不使用额外的线性布局也没有什么区别,因为我的所有活动都使用相同的基本布局,其他屏幕中的内容不仅仅是一个文本视图,所以在这里把它们拿出来是有意义的。我确实看到了一件不同的事情,那就是你的绘画作品的名字——‘about_title’和‘gradient’。它们是同一张画吗?哎呀!是的,这是我的错,因为我在测试使用@color引用是否有影响时将内容复制到新文件中。我将编辑原稿以更正此问题。
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:padding="10dip"
android:gravity="center"
android:orientation="vertical"
android:background="@drawable/tab_bg_selector">
<TextView
android:id="@+id/tab_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textColor="@drawable/tab_text_selector"/>
</LinearLayout>
TextView tvAboutUsTitle = (TextView) findViewById(R.id.AboutUsTitle);
tvAboutUsTitle.setBackgroundResource(R.drawable.about_title);