标签之间的间距不一致

时间:2011-11-16 03:23:44

标签: android android-layout tabs

有谁知道会导致什么?

enter image description here

enter image description here

我已经创建了一个自定义标签布局,我要么想要间距,要么没有,只是一致性。理想情况下,我不喜欢间距,但我读过这可能不适用于Android的操作栏。

这是我正在使用的xml代码:

 public class Tabs extends TabActivity {

private static final Properties properties = Properties.getInstance();
TabHost mTabHost;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabs);

    mTabHost = getTabHost();

    addTab(getString(R.string.text_tabs_1), R.drawable.listing_tab_active, new Intent(this, Main.class), R.drawable.listing_tab_inactive);
    addTab(getString(R.string.text_tabs_2), R.drawable.keyword_tab_active, new Intent(this, Keywords.class), R.drawable.keywords_tab_inactive);
    addTab(getString(R.string.text_tabs_3), R.drawable.saved_tab_active, new Intent(this, Saved.class), R.drawable.saved_tab_inactive);
}

private void addTab(String label, int drawableId, Intent intent, int imageId) 
{
    TabHost.TabSpec spec = mTabHost.newTabSpec(label);

    View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tabs_bg, getTabWidget(), false);

    ((TextView)tabIndicator.findViewById(R.id.tabText)).setText(label);

     ((ImageView)tabIndicator.findViewById(R.id.tabImage)).setImageResource(imageId);

    spec.setIndicator(tabIndicator);
    spec.setContent(intent);

    mTabHost.addTab(spec);      
}
 }

tabs.xml      

<LinearLayout
    android:orientation="vertical"
    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:padding="0dp" 
     />

     <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="0dp" 
     />

</LinearLayout>
 </TabHost>

tabs_bg.xml

 <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:padding="5dp"
android:background="@drawable/tab_bg_selector"
>
<ImageView android:id="@+id/tabImage"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:scaleType="center"
    android:layout_alignParentTop="true"
    android:paddingBottom="18dip"
/>
<TextView android:id="@+id/tabText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:textColor="@drawable/tab_text_selector"
    android:gravity="center_vertical|center_horizontal"
    android:textSize="12dip"
 />
 </RelativeLayout>

tab_bg_selector.xml

 <selector xmlns:android="http://schemas.android.com/apk/res/android">

<!--  Active tab -->
<item android:state_selected="true" android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/tab_bg_selected" />

<!--  Inactive tab -->
<item android:state_selected="false" android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/tab_bg_unselected" />

<!--  Pressed tab -->
<item android:state_pressed="true" android:drawable="@drawable/tab_bg_focused" />

<!--  Selected tab (using d-pad) -->
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_bg_focused" />

 </selector>

tab_bg_selected.xml

 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

<gradient android:startColor="#A8A8A8" android:centerColor="#7F7F7F" android:endColor="#696969" android:angle="-90" />

 </shape>

tab_bg_unselected.xml

 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">

<gradient android:startColor="#5C5C5C" android:centerColor="#424242" android:endColor="#222222" android:angle="-90" />

 </shape>

tab_bg_focused.xml

 <selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true" android:padding="0dp">
    <shape>
        <gradient
            android:startColor="#E77A26"
            android:endColor="#E77A26"
            android:angle="270" />
        <stroke
            android:width="3dp"
            color="#0F58A7" />

    </shape>
</item>

<item android:state_focused="true" android:padding="0dp">
    <shape>
        <gradient
            android:endColor="#0F58A7"
            android:startColor="#0F58A7"
            android:angle="270" />
        <stroke
            android:width="3dp"
            color="#0F58A7" />
    </shape>
</item>

<item android:padding="0dp">
    <shape>
        <gradient
            android:endColor="#0F58A7"
            android:startColor="#0F58A7"
            android:angle="270" />
        <stroke
            android:width="3dp"
            color="#0F58A7" />
    </shape>
</item>
 </selector> 

2 个答案:

答案 0 :(得分:0)

使用以下内容破解您自己的标签:

<?xml version="1.0" encoding="utf-8"?>
<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"
            android:background="#FFFFFF"> <!-- android:padding="5dp"  -->

        <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"


                android:layout_weight="1"/>

        <!--<FrameLayout-->
                <!--android:id="@+id/realtabcontent"-->
                <!--android:layout_width="fill_parent"-->
                <!--android:layout_height="wrap_content"-->
                <!--android:layout_weight="1"/>-->

        <LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal">
            <Button android:id="@+id/tab1"  android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/tab1_selector"  />
            <Button  android:id="@+id/tab2"
                     android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/tab2_selector"  />
            <Button android:id="@+id/tab3"     android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/tab3_selector"  />
            <Button android:id="@+id/tab4"    android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/tab4_selector"  />
            <Button android:id="@+id/tab5" android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/tab5_selector"  />

        </LinearLayout>


        <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:visibility="gone"/>

    </LinearLayout>

</TabHost>

答案 1 :(得分:0)

我明白了。将android:background =“@ drawable / tab_bg_selector”添加到TabWidget中修复了间距:

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:padding="0dp"
        android:background="@drawable/tab_bg_selector" 
     />