【问题标题】:How to use custom iPhone tab in FragmentActivity?如何在 FragmentActivity 中使用自定义 iPhone 选项卡?
【发布时间】:2012-11-02 10:11:23
【问题描述】:

我是 Android 新手,我尝试使用我在网上找到的代码中的 FragmentActivity 创建一个标签。 http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/

这是我的 FragmentActivity http://pastie.org/pastes/5170802/text?key=jhowuevxe2fshlwu5tisg

我想使用一个使用图像和文本的自定义布局。

//tab_indicator.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dip"
android:layout_height="55dip"    
android:layout_weight="1"
android:orientation="vertical"
android:background="@drawable/tab_indicator"
android:padding="5dp">

<ImageView android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:src="@drawable/icon"/> 

<TextView android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true"
    style="?android:attr/tabWidgetStyle"/>
</RelativeLayout>

我在这里发现了一个类似的问题:create custom tab in FragmentActivity 但我无法弄清楚如何在我的代码中应用该解决方案。有人可以教我怎么做吗?谢谢。

更新:

我设法在我的代码中增加了自定义布局。但我遇到了另一个错误。这是我最新的代码 http://pastie.org/pastes/5187362/text?key=74r87diquysvruwsam1tq 调整为 FragmentActivity,来自 AdilSoomro http://adilsoomro.blogspot.com/2011/06/iphone-like-tabs-in-android.html 代码(它使用 TabActivity),其中一些参考来自

我的最新代码成功地像这样膨胀了布局,其布局设置为http://pastie.org/pastes/5187408/text?key=qxxa5xxrhsburebllyhmw (tab_indicator.xml)

但我需要将标签对齐到底部。当我将它对齐到底部时,代码 layout_gravity="bottom" 在图形布局上工作,但是当我运行它时,我的相对布局的背景会填满整个屏幕,就像代码 http://pastie.org/pastes/5187445/text?key=6dz2tsiggey9se51d2thtq

谁能告诉我我做错了什么?

【问题讨论】:

  • 您在此处分享的上述参考代码中哪里遇到问题了。
  • @chintankhetiya 我不知道在我的代码中在哪里扩展我的布局。我是新手,所以使用片段比较模糊。
  • @chintankhetiya 嘿,我设法在代码中扩展了我的布局,但是当我将栏移到底部时,背景会填满页面。但是当我在图形布局中预览它时,背景不会填满页面。让我粘贴一些图像和我的布局代码
  • 顺便说一句,使用其他操作系统的设计模式不是一个好习惯。见:developer.android.com/design/patterns/pure-android.html
  • 是的@C.d。我知道这一点,这就是为什么我一直在努力解决它,并且很难为它获得资源,但我的项目需要我这样做。我无法逃避它,因为我必须满足一些要求。但是感谢您的信息:)

标签: android android-layout android-fragments android-tabhost android-fragmentactivity


【解决方案1】:

解决了!最后!在测试了我提到的其他资源后,我意识到 RelativeLayout 中的 layout_alignParentBottom 没有给出任何结果。因此,我尝试将 TabWidget 对齐到底部,并且成功了!

这是我的 main.xml,其中包含 TabHost 和 TabWidget。 http://pastie.org/pastes/5188297/text?key=lqfp3jnofs5kgsvslm3yg

保留 tab_indicator.xml / 文本和图像的设置,如下所示:http://pastie.org/pastes/5187408/text?key=qxxa5xxrhsburebllyhmw

这是一个使用 FragmentActivity 的自定义 iPhone 选项卡:

P/S : 抱歉,如果我的 Java 文件令人困惑,我是新手,如果我做错了什么,请告诉我。

【讨论】:

    【解决方案2】:

    试试这个蜂蜜>

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="55dp"    
        android:layout_weight="1"
        android:orientation="vertical" 
        android:layout_gravity="bottom"  
        android:background="@android:drawable/title_bar"
        android:padding="5dp"> 
    
        <TextView android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" 
            android:layout_centerHorizontal="true"
            style="?android:attr/tabWidgetStyle"/>
    
        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_action_search" />
    
          <ImageView
              android:id="@+id/icon"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:layout_centerVertical="true"
              android:layout_marginLeft="49dp"
              android:src="@drawable/ic_action_search" />
    
    </RelativeLayout>
    

    【讨论】:

    • 在不同的文件中测试了我的代码。它应该在下面对齐。我的猜测是,我的 java 代码可能有问题
    猜你喜欢
    • 1970-01-01
    • 2016-02-03
    • 2017-12-22
    • 2019-11-19
    • 1970-01-01
    • 2018-04-16
    • 1970-01-01
    • 2022-11-10
    • 2016-01-31
    相关资源
    最近更新 更多