开发者

How to add TabHost to a LinearLayout bu code in android?

开发者 https://www.devze.com 2023-03-09 12:07 出处:网络
For my android application I need to implement a tabView within a LinearLayout. I could able to add textview and button to the LinearLayout like this,

For my android application I need to implement a tabView within a LinearLayout. I could able to add textview and button to the LinearLayout like this,

public CreateView(Context context) {
    super(context);
    LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    Button button = new Button(context);
    button.setText("Submit");
    button.setLayoutParams(llp);
    TextView tv = new TextView(context);
    tv.setText("This is a test");
    tv.setLayoutParams(llp);
    ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
    this.addView(tv);
    this.addView(button);}

In my Activity class I added these as,

public class MyLinearLayout extends Activity {
LinearLayout ll;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ll = new LinearLayout(this);

    ll.addView(new CreateView(this));
    setContentView(ll);

}

}

I want to do the same thing for a tabview. But I couldn't able to find a way to add tabHost to my lineaLayout. any way t开发者_Go百科o do this?? thanx


When defining the TabHost in xml and adding tabs programmatically with tabHost.newTabSpec like it's shown in tutorial, the following structure is created:

LinearLayout
     TabHost>
          LinearLayout
               TabWidget
               FrameLayout
                   LinearLayout for tab 1
                   LinearLayout for tab 2
                   ...

I guess you need to replicate this nesting in code.
The root-LinearLayout in the xml-version has width and height set to fill_parent and orientation to vertical. Maybe that's also required for it to work.

0

精彩评论

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