【问题标题】:Tabhost to launch new page via intentTabhost 通过意图启动新页面
【发布时间】:2014-02-25 09:39:23
【问题描述】:

我正在尝试在选项卡选择上打开新页面... 当我启动项目时它突然崩溃...

这里是源... (Log Error是你的内容必须有一个id属性为R.id.tabhost的tabshost)

 public class AddFriends_Activity extends TabActivity implements SwipeInterface {
TabHost mTabHost;
 Resources res = getResources();
        Intent i = new Intent(this,Added_Friends.class);
        Intent j = new Intent(this,Friend_List.class);
        TabHost mTabHst = getTabHost();
 mTabHst.addTab(mTabHst.newTabSpec("tab_test1").setIndicator("Add Frinds",res.getDrawable(R.drawable.add1))
                .setContent(i));

     mTabHst.addTab(mTabHst.newTabSpec("tab_test2").setIndicator("Friend List",res.getDrawable(R.drawable.add1))
                .setContent(j));


         int tabCount = mTabHst.getTabWidget().getTabCount();
         for (int r = 0; r < tabCount; r++) {
             final View view = mTabHst.getTabWidget().getChildTabViewAt(r);
             if ( view != null ) {
                 // reduce height of the tab
                 view.getLayoutParams().height *= 0.90;

                 //  get title text view
                 final View textView = view.findViewById(android.R.id.title);
                 if ( textView instanceof TextView ) {

                     ((TextView) textView).setGravity(Gravity.BOTTOM);
                     ((TextView) textView).setTextSize((float) 12);
                     ((TextView) textView).setTextColor(android.graphics.Color.WHITE);

                     ((TextView) textView).setSingleLine(false);

                     // explicitly set layout parameters
                     textView.getLayoutParams().height = ViewGroup.LayoutParams.FILL_PARENT;
                     textView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
                 }
             }
         }

         mTabHst.setCurrentTab(0);

【问题讨论】:

标签: android android-intent android-tabhost


【解决方案1】:

答案终于来了……

 Resources res = getResources();
        Intent i = new Intent(this,Added_Friends.class);
        Intent j = new Intent(this,Friend_List.class);
        TabHost mTabHst = getTabHost();
        mTabHst.addTab(mTabHst.newTabSpec("tab_test1").setIndicator(null,res.getDrawable(R.drawable.ic_launcher))
                .setContent(i));

     mTabHst.addTab(mTabHst.newTabSpec("tab_test2").setIndicator(null,res.getDrawable(R.drawable.ic_launcher))
                .setContent(j));


         int tabCount = mTabHst.getTabWidget().getTabCount();
         for (int r = 0; r < tabCount; r++) {
             final View view = mTabHst.getTabWidget().getChildTabViewAt(r);
             if ( view != null ) {
                 // reduce height of the tab
                 view.getLayoutParams().height *= 0.90;

                 //  get title text view
                 final View textView = view.findViewById(android.R.id.title);
                 if ( textView instanceof TextView ) {

                     ((TextView) textView).setGravity(Gravity.CENTER);
                     ((TextView) textView).setTextSize((float) 12);
                     ((TextView) textView).setTextColor(android.graphics.Color.BLACK);

                     ((TextView) textView).setSingleLine(false);

                     // explicitly set layout parameters
                     textView.getLayoutParams().height = ViewGroup.LayoutParams.FILL_PARENT;
                     textView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
                 }
             }
         }

         mTabHst.setCurrentTab(0);

在您的布局中添加以下 xml...

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="5dip">


    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_below="@+id/tabcontent"
        android:layout_gravity="center"
        android:gravity="center"  />
      <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@android:id/tabs"
        android:layout_marginBottom="60dp">

    </FrameLayout>
</RelativeLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 2016-10-10
    • 2023-03-22
    • 1970-01-01
    • 2012-12-17
    相关资源
    最近更新 更多