加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Tabs不占用屏幕的整个宽度

发布时间:2020-12-15 05:00:50 所属栏目:Java 来源:网络整理
导读:我正在片段类中创建选项卡.现在我在片段类中显示两个选项卡.一切正常,标签显示正确.只有高速缓存,显示的选项卡只占屏幕宽度的一半,它不占用全屏宽度. 所以任何人都告诉我在我的代码中我需要改变什么才能实现这一目标 我的守则 tabsinfo_fragment.xml文件 ?xm
我正在片段类中创建选项卡.现在我在片段类中显示两个选项卡.一切正常,标签显示正确.只有高速缓存,显示的选项卡只占屏幕宽度的一半,它不占用全屏宽度.

所以任何人都告诉我在我的代码中我需要改变什么才能实现这一目标

我的守则

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>

片段类中的代码

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,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();
        }
    }
}

截图

解决方法

TabWidget类是LinearLayout的子类,因此在根xml元素的tab.xml文件中,在您的情况下为LinearLayout,将width设置为0dp,将weight设置为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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读