本文介绍了添加选择图像fragmentTabHost的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要选择添加到标签。像select_image和unselect_image为每个标签。我做了这个样子。

这个特殊的code正在运行TabActivity

 公共类MainActivity扩展FragmentActivity
{
    私人FragmentTabHost mTab​​Host;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState)
    {
        super.onCreate(savedInstanceState);        的setContentView(R.layout.activity_main);        mTabHost =(FragmentTabHost)findViewById(android.R.id.tabhost);        mTabHost.setup(这一点,getSupportFragmentManager(),R.id.realtabcontent);
        则tabspec规范;        意图意图;        查看saveView = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab_save_btn,NULL);        意图=新意图(MainActivity.this,Save.class);        规格= mTab​​Host.newTabSpec(保存)。setIndicator(saveView)
                .setContent(意向);        mTabHost.addTab(规范);        查看chargeView = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab_charge_btn,NULL);        意图=新意图(MainActivity.this,Charge.class);        规格= mTab​​Host.newTabSpec(CHARGE)。setIndicator(chargeView)
                .setContent(意向);        mTabHost.addTab(规范);
        查看rankView = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab_rank_btn,NULL);        意图=新意图(MainActivity.this,RankFragment.class);        规格= mTab​​Host.newTabSpec(等级)。setIndicator(rankView)
                .setContent(意向);        mTabHost.addTab(规范);        mTabHost.setCurrentTab(1);    }
}

tab_save_btn.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT>    < ImageView的
        机器人:ID =@ + ID / save_tab
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_centerInParent =真
        机器人:背景=@绘制/ save_selector/>< / RelativeLayout的>

save_selector

 <?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>    <项目机器人:可绘制=@绘制/ home_selected机器人:state_selected =真正的>< /项目>
    <项目机器人:可绘制=@绘制/ home_unselect机器人:state_selected =假>< /项目>< /选择>

这是给我的多个错误。同样的code上的TabActivity。


  1. 错误说要tabhost.setUp必须调用了语境和
    FragmentManager作为参数。

你忘记调用tabhost.setup(LocalActivityManager的ActivityGroup)

我在哪里做错了?

activity_main.xml中

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>    <的FrameLayout
        机器人:ID =@ + ID / realtabcontent
        机器人:layout_width =match_parent
        机器人:layout_height =0dip
        机器人:layout_weight =1/>    < android.support.v4.app.FragmentTabHost
        机器人:ID =@机器人:ID / tabhost
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT>        <的FrameLayout
            机器人:ID =@机器人:ID / tabcontent
            机器人:layout_width =0dp
            机器人:layout_height =0dp
            机器人:layout_weight =0/>
    < /android.support.v4.app.FragmentTabHost>< / LinearLayout中>


解决方案

试试这个

  mTab​​Host =(FragmentTabHost)findViewById(android.R.id.tabhost);
             mTabHost.setup(这一点,getSupportFragmentManager(),R.id.realtabcontent);
         查看saveView = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab_save_btn,NULL);
        mTabHost.addTab(mTabHost.newTabSpec(保存)setIndicator(saveView),SaveFragment.class,空);
      查看chargeView = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab_charge_btn,NULL);        mTabHost.addTab(mTabHost.newTabSpec(CHARGE)setIndicator(chargeView),ChargeFragment.class,空);
    .....
    ...........        mTabHost.setCurrentTab(1);

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 andFragmentManager 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>
解决方案

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);

这篇关于添加选择图像fragmentTabHost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-14 21:44