【问题标题】:In android, how can I put tabs in a view containing an image on top?在android中,如何将标签放在顶部包含图像的视图中?
【发布时间】:2011-06-17 18:08:06
【问题描述】:

我正在创建一个 android 应用程序并尝试在图片和一些文本下方添加三个选项卡。

 ______________________________
 |  _____________             |
 |  |           |   text      |
 |  | picture   |   text      |
 |  |           |   text      |
 |  —————————————             |
 |  _______ ________ _______  |
 |  | tab | |  tab | | tab |  |
 |  ————————————————————————  |
 |  |                      |  |
 |  |                      |  |
 |  |       content        |  |
 |  |        here          |  |
 |  |                      |  |
 |  |                      |  |
 |                            |
 ——————————————————————————————

我不知道具体是如何做到这一点的。 任何想法将不胜感激。 谢谢。

【问题讨论】:

    标签: java android android-layout tabs android-tabhost


    【解决方案1】:
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">
      <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <ImageView 
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="image_source/>
        <LinearLayout
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
          <TextView
            android:id="@android:id/text1"
            android:text="some text"
            android:layout_width="wrap_content"
          android:layout_height="wrap_content" />
          <TextView
            android:id="@android:id/text2"
            android:text="some text"
            android:layout_width="wrap_content"
          android:layout_height="wrap_content" />
          <TextView
            android:id="@android:id/text3"
            android:text="some text"
            android:layout_width="wrap_content"
          android:layout_height="wrap_content" />
        </LinearLayout>
      </LinearLayout>
      <TabHost
        android:id="@+id/my_tab_viewer"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp">
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="5dp" />
        </LinearLayout>
      </TabHost>
    </LinearLayout>
    

    这应该为您提供基本结构,然后在您的活动中添加您的标签:

    TabHost tabHost = (TabHost) findViewById(R.id.my_tab_viewer);
    TabHost.TabSpec spec = tabHost.newTabSpec("artists").setIndicator("Artists",
                      res.getDrawable(R.drawable.ic_tab_artists))
                  .setContent(intent);
    tabHost.addTab(spec);
    

    取自http://developer.android.com/resources/tutorials/views/hello-tabwidget.html的Java源码示例

    您可能需要调整 XML 中每个组件的 layout_width、layout_height,我没有测试该布局。

    【讨论】:

      【解决方案2】:

      听起来像 Android TabHost 小部件是您在标准 Activity 子类中寻找的东西(Android 的 TabActivity 对于您提议的布局来说可能太不灵活了)。

      TabHost 是一个提供标签栏的小部件,带有一个 FrameLayout 来显示每个标签的内容。您可以使用 RelativeLayout(首选)或 LinearLayout 将其放在布局中的图片/文本下方。

      【讨论】:

      • 好的。谢谢你。我知道我需要使用 TabHost。我需要制作主布局 TabHost 还是可以使用 FrameLayout 并将 TabHost 放入其中。如果我确实需要将主布局设置为 TabHost,那么如何将选项卡放在不变的图像和文本下方。
      • 您在图表中标记为“此处的内容”的区域是您嵌入在 TabHost 中的 FrameLayout。 TabHost 还要求您嵌入 TabWidget 以显示选项卡。 Google 在他们的网站上提供了一个教程 (developer.android.com/resources/tutorials/views/…)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-04
      • 1970-01-01
      • 2014-12-26
      • 1970-01-01
      • 2020-07-19
      • 2011-12-24
      相关资源
      最近更新 更多