开发者

Why would a drawable inside a tab view not be visible?

I have a custom TabHost that adds tabs like this

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);
}

where mTabHost is the tab host and the tabs_bg.xml just has a textview in a linearlayout. (My main layout is the same as the Tab Layout example; I'm just trying to have small, text only tabs.) My info tab is invoked like this:

intent = new Intent().setClass(this, AboutScreen.class);
setTab(new 开发者_Go百科TextView(this), "about", intent);

AboutScreen extends Activity, and all it does is setContentView to 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>

where @drawable/about_title is this:

<?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>

That drawable isn't being displayed inside the FrameLayout. Everything else shows up correctly. Any ideas what I'm doing wrong?


EDIT: if I set it programmatically

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

it shows up. Why is that different than setting it in the xml?


i can't see you using tv (TextView) anywhere, unless its just missing from that snippet of code?

Here is an example of how i setup 3 tabs in an activity which might help you

EDIT: I forgot to mention. In this code i also needed what i think you want, a custom tab of a single line.

/** 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>


I've had problems where a view is not displayed large enough due to it size seemingly being determined before it's contents are known, even though it's all held in xml (with references out to strings)

Have you tried setting the layout_height and widths to numerical values (or even 'fill parent' to see if that is the case?

Also, I would simplify the layout to remove the nested LinearLayouts? as they appear unnecessary due to each only holding one TextView


Since we're not using the tab view now anyway, I'm going to close this, since this

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

fixed the problem I was having anyway, even if I don't really know why.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜