【问题标题】:Why would a drawable inside a tab view not be visible?为什么选项卡视图中的可绘制对象不可见?
【发布时间】: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);
}

其中 mTab​​Host 是选项卡主机,而 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


【解决方案1】:

我看不到你在任何地方使用电视(TextView),除非它只是从代码的 sn-p 中丢失?

这是我如何在活动中设置 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>

【讨论】:

  • 我在获得 textview 后立即执行tv.setText(tag);。我的 tabs_bg.xml 看起来与您的 tab_item.xml 相同(我的 @+id/tabsText 是您的 @+id/tab_label。)如果我将 AboutScreen 称为主要活动而不是选项卡布局,则会显示渐变。如果我在选项卡布局中调用它,它不会。
【解决方案2】:

我遇到的问题是视图显示不够大,因为它的大小似乎在其内容已知之前就已确定,即使它全部保存在 xml 中(引用字符串)

您是否尝试将 layout_height 和 widths 设置为数值(或者甚至“填充父级”以查看是否是这种情况?

另外,我会简化布局以删除嵌套的 LinearLayouts 吗?因为它们看起来没有必要,因为每个都只持有一个 TextView

【讨论】:

  • 我尝试将 layout_height/widths 更改为数值,包括 dp 和 px,但这没有区别 - 如果您直接查看活动,它们会显示可绘制对象,但当它加载到选项卡,除非您以编程方式专门设置它。如果我在有或没有无关的 LinearLayouts 的情况下执行此操作也没有什么区别(这些只是因为我所有的活动都使用相同的基本布局,而其他屏幕中的内容不仅仅是一个 textview,所以把它们放在这里是有道理的。 )
【解决方案3】:

既然我们现在不使用标签视图,我将关闭它,因为这个

TextView tvAboutUsTitle = (TextView) findViewById(R.id.AboutUsTitle);
tvAboutUsTitle.setBackgroundResource(R.drawable.about_title);

解决了我遇到的问题,即使我真的不知道为什么。

【讨论】:

    猜你喜欢
    • 2015-06-14
    • 2019-02-13
    • 2016-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    • 2021-06-01
    相关资源
    最近更新 更多