2013-12-12 4 views
0

Я использую Tabhost, где я хочу установить фоновое изображение. Но после установки, он приходит как этотКак правильно выравнивать фоновое изображение для TabHost?

! [Введите описание изображения здесь] [1]

Как я могу установить его полностью? Ниже приведен код:

/* Tabs */ 
     Bundle bundle = getIntent().getExtras(); 
     TabHost tabHost = getTabHost(); 
     TabHost.TabSpec spec; 
     Intent intent; 

     // First Activity 
     intent = new Intent().setClass(this, InfoListView.class); 
     //spec = tabHost.newTabSpec("some_things").setIndicator("Info").setContent(intent); 
     spec = tabHost.newTabSpec("top_things").setIndicator("Info",getResources().getDrawable(R.drawable.tabicon)).setContent(intent); 
     tabHost.addTab(spec); 

     // Second Activity 
     intent = new Intent().setClass(this, LogListView.class); 
     /*mContext = getApplicationContext(); 
     TextView mTv = new TextView(mContext); 
     mTv.setText("Sync Log"); 
     mTv.setBackgroundDrawable(mContext.getResources.getDrawable(R.drawable.tabicon)); 
     spec = tabHost.newTabSpec("top_things").setIndicator(mTv).setContent(intent);*/ 
     spec = tabHost.newTabSpec("top_things").setIndicator("Sync Log", getResources().getDrawable(R.drawable.tabicon)).setContent(intent); 
     tabHost.addTab(spec); 

     tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 95; 
     tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 95; 

/* Вкладки заканчивается */

XML

<TabHost 
     android:id="@android:id/tabhost" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_below="@+id/title" > 

     <RelativeLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:padding="0dp" > 

      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentTop="true" /> 

      <View 
       android:id="@+id/separator" 
       android:layout_width="fill_parent" 
       android:layout_height="2dip" 
       android:layout_below="@android:id/tabs" /> 

      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_above="@+id/btnSend" 
       android:layout_below="@+id/separator" 
       android:paddingBottom="45dip" > 

       <!-- Scrollview for message data --> 

       <ScrollView 
        android:id="@+id/formTab" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:scrollbars="vertical" > 

        <LinearLayout 
         android:id="@+id/formLayout" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:orientation="vertical" > 

         <View 
          android:layout_width="fill_parent" 
          android:layout_height="5dip" /> 
        </LinearLayout> 
       </ScrollView> 
      </FrameLayout> 
     </RelativeLayout> 
    </TabHost> 
+0

решена проблема ур? –

+0

Да, он решил. Мне пришлось использовать Text View, а затем setBackground для него – user45678

+0

ok nice. если вы решили это самостоятельно. отправьте свой ответ, чтобы, если у кого-то была такая же проблема. возьмите справку из вашего ответа. –

ответ

0

я получил ответ: Это может быть сделано, как это:

TabHost tabHost = getTabHost(); // The activity TabHost 
       TabHost.TabSpec spec; // Resusable TabSpec for each tab 
       Intent intent; // Reusable Intent for each tab 
       // Create an Intent to launch an Activity for the tab (to be reused) 
       intent = new Intent().setClass(this, InboxActivity.class); 
       // Create our custom view. 
       View tabView = createTabView(this, "Inbox"); 
       // Initialize a TabSpec for each tab and add it to the TabHost 
       spec = tabHost.newTabSpec("Inbox").setIndicator(tabView) 
         .setContent(intent); 
       tabHost.addTab(spec); 
       // Do the same for the other tabs 
       tabView = createTabView(this, "Outbox"); 
       intent = new Intent().setClass(this, OutboxActivity.class); 
       spec = tabHost.newTabSpec("Outbox").setIndicator(tabView) 
         .setContent(intent); 
       tabHost.addTab(spec); 

Метод

private static View createTabView(Context context, String tabText) { 
     View view = LayoutInflater.from(context).inflate(R.layout.custom_tab, null, false); 
     TextView tv = (TextView) view.findViewById(R.id.tabTitle); 
     tv.setText(tabText); 
     return view; 
    } 

Custom_tab

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/tabTitle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" 
      android:clickable="true" 
      android:padding="5dp" 
      android:textSize="15sp" 
      android:textStyle="bold" 
      android:textColor="#FFFFFF" 
      android:background="@drawable/tab_selector"/> 

tab_selector

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="true" android:state_pressed="false" 
      android:drawable="@drawable/tabbackground" /> 
    <item android:state_selected="false" android:state_pressed="false" 
      android:drawable="@drawable/tabnormal" /> 
    <item android:state_pressed="true" 
      android:drawable="@drawable/tabbackground" /> 
</selector> 
0

Добавьте этот код android:gravity="center" или android:layout_gravity="center" в вашем XML в той части, где вы добавления изображений.

Или Добавить-

spec.setGravity(Gravity.CENTER); 

Перед добавлением его.

0

После того как ваш tabhost является intilize с индикатором и других вещей, добавить фон к нему, как этот

tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tabicon); //fro first tab 
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tabicon); //for second tab   
Смежные вопросы