How to use custom iPhone tab in FragmentActivity?

浪子不回头ぞ 提交于 2019-12-11 09:43:01

问题


I am new in Android and I tried to create a tab using FragmentActivity from codes that I found online. http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/

This is my FragmentActivity http://pastie.org/pastes/5170802/text?key=jhowuevxe2fshlwu5tisg

I would like to use a custom layout that uses an image and text.

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

I found a similar question here : create custom tab in FragmentActivity but I couldn't figure it out how to apply the solution in my codes. Can someone teach me how? Thank you.

Update:

I managed to inflate the custom layout in my codes. But I faced another error. This is my latest codes http://pastie.org/pastes/5187362/text?key=74r87diquysvruwsam1tq tweaked to FragmentActivity, from AdilSoomro http://adilsoomro.blogspot.com/2011/06/iphone-like-tabs-in-android.html codes (which uses TabActivity) with some references from

  • http://android.codeandmagic.org/2011/07/android-tabs-with-fragments/
  • http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/

My latest codes successfully inflate the layout like this, with the setting http://pastie.org/pastes/5187408/text?key=qxxa5xxrhsburebllyhmw for its layout (tab_indicator.xml)

but I need to align my tab to the bottom. When I align it to the bottom, the code layout_gravity="bottom" worked on Graphical Layout but when I run it, my relative layout's background fill the whole screen, like this with the code http://pastie.org/pastes/5187445/text?key=6dz2tsiggey9se51d2thtq

can someone tell me what I did wrong?


回答1:


Solved it! Finally! After testing other resources that I referred to, I realized that layout_alignParentBottom in RelativeLayout does not give any outcome. Hence I tried aligning the TabWidget to the bottom instead, and it worked!

This is my main.xml that contain TabHost and TabWidget. http://pastie.org/pastes/5188297/text?key=lqfp3jnofs5kgsvslm3yg

Keep the tab_indicator.xml / the settings for the text and image like this : http://pastie.org/pastes/5187408/text?key=qxxa5xxrhsburebllyhmw

And here is the a custom iPhone tab that uses FragmentActivity :

P/S : Sorry if my Java file is confusing, I'm new and if there is anything I did wrong, do tell me.




回答2:


try this Honey >

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


来源:https://stackoverflow.com/questions/13193189/how-to-use-custom-iphone-tab-in-fragmentactivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!