java - 选项卡不占据屏幕的整个宽度

标签 java android android-tabs

我正在 fragment 类中创建选项卡。现在我在 fragment 类中显示两个选项卡。一切正常,选项卡显示正确。唯一的缓存是,显示的标签只占屏幕宽度的一半,而不是全屏宽度。

所以有人告诉我我需要在我的代码中更改什么才能实现这一点

我的代码

tabsinfo_fragment.xml 文件

<?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"
    android:background="#EFEFEF" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <HorizontalScrollView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true"
            android:scrollbars="none" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </HorizontalScrollView>

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

            <FrameLayout
                android:id="@+id/tab_1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />

            <FrameLayout
                android:id="@+id/tab_2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
        </FrameLayout>
    </LinearLayout>

</TabHost>

tab.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="20dp"  
    android:background="@drawable/tab_selector">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold"
        android:textColor="@drawable/tab_text_selector"
        android:textIsSelectable="false" />
</LinearLayout>

fragment 类中的代码

public class TabsInfoFragment extends Fragment implements OnTabChangeListener
{
    private View        m_Root;

    private TabHost     m_TabHost;

    private int         m_CurrentTab;

    public  String      m_VisitorTabText;

    public  String      m_FeedTabText;

    public TabsInfoFragment() 
    {

    }

    @Override
    public void onAttach(Activity activity) 
    {
        super.onAttach(activity);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        m_VisitorTabText = Integer.toString(R.string.tab_visitor_text);
        m_FeedTabText = Integer.toString(R.string.tab_feed_text);

        m_Root  = inflater.inflate(R.layout.tabsinfo_fragment, null);
        m_TabHost = (TabHost) m_Root.findViewById(android.R.id.tabhost);        
        setupTabs();

        return m_Root;  
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) 
    {
        super.onActivityCreated(savedInstanceState);
        setRetainInstance(true);

        m_TabHost.setOnTabChangedListener(this);
        m_TabHost.setCurrentTab(m_CurrentTab);

        // Manually start loading stuff in the first tab
        updateTab(m_VisitorTabText, R.id.tab_1, new ProfileInfoFragment());
    }

    private void setupTabs() 
    {
        m_TabHost.setup(); 
        m_TabHost.addTab(newTab(m_VisitorTabText, R.string.tab_visitor_text, R.id.tab_1));
        m_TabHost.addTab(newTab(m_FeedTabText, R.string.tab_feed_text, R.id.tab_2));
    }

    private TabSpec newTab(String tag, int labelId, int tabContentId) 
    {       
        View indicator = LayoutInflater.from(getActivity()).inflate(R.layout.tab, (ViewGroup) m_Root.findViewById(android.R.id.tabs), false);
        ((TextView) indicator.findViewById(R.id.text)).setText(labelId);

        TabSpec tabSpec = m_TabHost.newTabSpec(tag);
        tabSpec.setIndicator(indicator);
        tabSpec.setContent(tabContentId);
        return tabSpec;
    }

    @Override
    public void onTabChanged(String tabId) 
    {
        if (m_VisitorTabText.equals(tabId)) 
        {
            updateTab(tabId, R.id.tab_1, new ProfileInfoFragment());
            m_CurrentTab = 0;
            return;
        }
        if (m_FeedTabText.equals(tabId)) 
        {
            updateTab(tabId, R.id.tab_2, new ProfileInfoFragment());
            m_CurrentTab = 1;
            return;
        }
    }

    private void updateTab(String tabId, int placeholder, Fragment fragmentClass) 
    {
        FragmentManager fm = getFragmentManager();
        if (fm.findFragmentByTag(tabId) == null) 
        {
            fm.beginTransaction().replace(placeholder, fragmentClass, tabId).commit();
        }
    }
}

截图

enter image description here

最佳答案

TabWidget 类是 LinearLayout 的子类,所以在根 xml 元素的 tab.xml 文件中,在您的情况下是 LinearLayout,将宽度设置为 0dp,权重设置为 1,那么所有选项卡的宽度都将相等.像这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="50dp"
    android:gravity="center">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:text="This is a tab" />

</LinearLayout>

enter image description here

关于java - 选项卡不占据屏幕的整个宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16809638/

相关文章:

Android TabLayout 不重新加载 fragment

java - ClassNotFoundException com.sun.mail.util.SharedByteArrayInputStream

java - 从 CentOS 7 终端运行 java 时出现 ClassNotFound 异常

java - 什么是java文档中的未归属作者

Android:如何在不同设备上绘制相同大小的ImageView?

android - 如果我们必须在 android 中使用 viewpager 创建多个页面,那么我们是否必须创建多个 fragment ?还有其他选择吗?

java - 如何读取 HOCON 配置以将值加载到 Map 中?

java - 在android中合并音频和视频文件

android - 为什么我不能在 ListView 之后放置 Adview?

android - 使用 PagerAdapter 开始新 Activity