2011-11-15 3 views
3

나는 안드로이드에 탭 레이아웃을 구현 중입니다. 내 구현은 출력 다음 부여합니다 그러나 탭을 구현할 수 있어요 :안드로이드 탭 : 상단에 텍스트 쓰기

이 이미지에서

enter image description here

는 텍스트 "표 1" "탭이" "탭 (3)은"모두 탭의 하단에 기록됩니다. 나는 그것을 맨 위에 쓰길 원합니다. 이미 과 android:layout_gravity="top"을 사용했지만 나에게는 아무런 영향이 없었습니다. 도와주세요. 다음 레이아웃을 사용하고 있습니다.

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tabhost" 
    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" 
     android:gravity="top" 
      android:layout_gravity="top" 
     > 
     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="top" 
      android:layout_gravity="top" 
      /> 
     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:padding="5dp" /> 
    </LinearLayout> 
</TabHost> 

도와주세요.

답변

1

사용자 정의보기로 TabSpec 콘텐츠를 설정하여이 작업을 수행 할 수 있습니다. 여기에 샘플이 있습니다. tabs_bg.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tabsLayout" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:background="@drawable/tab_bg_selector" 
    android:padding="10dip" android:gravity="center" android:orientation="vertical"> 

    <TextView android:id="@+id/tabsText" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:text="Title" 
     android:textSize="15dip" android:textColor="@drawable/tab_text_selector" /> 
</LinearLayout> 

에서 main.xml에

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@android:id/tabhost" android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <LinearLayout android:orientation="vertical" 
      android:layout_width="fill_parent" android:layout_height="fill_parent"> 
      <View android:layout_width="fill_parent" android:layout_height="0.5dip" 
       android:background="#000" /> 
      <TabWidget android:id="@android:id/tabs" 
       android:layout_width="fill_parent" android:layout_height="wrap_content" 
       android:layout_marginLeft="0dip" android:layout_marginRight="0dip" /> 
      <View android:layout_width="fill_parent" android:layout_height="2dip" 
       android:background="#696969" /> 
      <View android:layout_width="fill_parent" android:layout_height="2dip" 
       android:background="#000" /> 
      <FrameLayout android:id="@android:id/tabcontent" 
       android:layout_width="fill_parent" android:layout_height="fill_parent" /> 
     </LinearLayout> 
    </TabHost> 

</LinearLayout> 

에서

내가보기에 대해 일부 사용자 지정 선택기를 사용, 당신은 자신에 만들 수 있습니다. 활동 Main.class가에서

,이 같은 출력을 제공

private TabHost mTabHost; 

private void setupTabHost() { 
    mTabHost = (TabHost) findViewById(android.R.id.tabhost); 
    mTabHost.setup(); 
} 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // construct the tabhost 
    setContentView(R.layout.main); 

    setupTabHost(); 
    mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider); 

    setupTab(new TextView(this), "Tab 1"); 
    setupTab(new TextView(this), "Tab 2"); 
    setupTab(new TextView(this), "Tab 3"); 
} 

private void setupTab(final View view, final String tag) { 
    View tabview = createTabView(mTabHost.getContext(), tag); 

    TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(new TabContentFactory() { 
     public View createTabContent(String tag) {return view;} 
    }); 
    mTabHost.addTab(setContent); 

} 

private static View createTabView(final Context context, final String text) { 
    View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null); 
    TextView tv = (TextView) view.findViewById(R.id.tabsText); 
    tv.setText(text); 
    return view; 
} 

... enter image description here

+0

감사 카디는, 당신의 코드에서 힌트를 취함으로써 내 문제를 해결했다. 다시 한 번 감사드립니다. –

관련 문제