Add selector image to fragmentTabHost

不打扰是莪最后的温柔 提交于 2020-01-17 12:38:06

问题


I want to add selectors to tabs. like select_image and unselect_image for each tab. I did it like this.

This particular code is running for TabActivity

public class MainActivity extends FragmentActivity
{
    private FragmentTabHost mTabHost;

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

        setContentView(R.layout.activity_main);

        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);

        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);


        TabSpec spec;

        Intent intent;

        View saveView = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab_save_btn,null);



        intent = new Intent(MainActivity.this, Save.class);

        spec = mTabHost.newTabSpec("SAVE").setIndicator(saveView)
                .setContent(intent);

        mTabHost.addTab(spec);



        View chargeView = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab_charge_btn, null);

        intent = new Intent(MainActivity.this, Charge.class);

        spec = mTabHost.newTabSpec("CHARGE").setIndicator(chargeView)
                .setContent(intent);

        mTabHost.addTab(spec);


        View rankView = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab_rank_btn, null);

        intent = new Intent(MainActivity.this, RankFragment.class);

        spec = mTabHost.newTabSpec("RANK").setIndicator(rankView)
                .setContent(intent);

        mTabHost.addTab(spec);

        mTabHost.setCurrentTab(1);

    }
}

tab_save_btn.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/save_tab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/save_selector" />

</RelativeLayout>

save_selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/home_selected" android:state_selected="true"></item>
    <item android:drawable="@drawable/home_unselect" android:state_selected="false"></item>

</selector>

it is giving me multiple error . This same code is running for TabActivity.

  1. Error says to tabhost.setUp must call that takes Context and FragmentManager as parameter.

Did you forget to call tabhost.setup(LocalActivityManager activityGroup)

Where am i doing wrong?

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />

    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

回答1:


try this

    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
             mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
         View saveView = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab_save_btn,null);
        mTabHost.addTab(mTabHost.newTabSpec("SAVE").setIndicator(saveView ),SaveFragment.class, null);
      View chargeView = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab_charge_btn, null);

        mTabHost.addTab(mTabHost.newTabSpec("CHARGE").setIndicator(chargeView ),ChargeFragment.class, null);
    .....
    ...........

        mTabHost.setCurrentTab(1); 


来源:https://stackoverflow.com/questions/28047245/add-selector-image-to-fragmenttabhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!