如何在每个活动中保持标签可见
Posted
技术标签:
【中文标题】如何在每个活动中保持标签可见【英文标题】:how to keep tabs visible on every activity 【发布时间】:2013-07-28 21:33:38 【问题描述】:我对 android 编程非常陌生,我正在尝试使用片段来实现选项卡。 我正在使用这个Tutorial。一切顺利,但我的问题是我想打开一个新活动,并且在这个新活动上我想保留标签栏。我希望在选项卡内打开的每个新活动上都可以看到选项卡栏。我有以下代码..
public class Tab1Fragment extends Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
if (container == null)
return null;
LinearLayout theLayout = (LinearLayout) inflater.inflate(
R.layout.tab_frag1_layout, container, false);
// Register for the Button.OnClick event
Button b = (Button) theLayout.findViewById(R.id.btn_startActivity);
b.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Toast.makeText(Tab1Fragment.this.getActivity(),
"open new activity with tab bar", Toast.LENGTH_LONG).show();
//Here I want to start new activity with tab bar
);
return theLayout;
// return (LinearLayout)inflater.inflate(R.layout.tab_frag1_layout,
// container, false);
【问题讨论】:
我猜,当点击按钮 btn_startActivity 时,新的 Activity 会全屏打开并且没有实现 TAB 控件,这些控件已经在Tab1Fragment
中实现了 - 对吧?
我没有确切的答案,但您可能希望将所有活动转换为片段。这可能会给你你想要的。
嗨,GOODY,是的,你是对的。新活动打开时没有标签。
嗨 Shobhit,你能解释一下你想说什么吗?
【参考方案1】:
这是主要活动。
package com.example.tabs;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.sample.fragments.R;
public class FragmentTabs extends SherlockFragmentActivity implements FragmentChangeListener
private TabHost mTabHost;
private int mContainerId;
private FragmentTransaction fragmentTransaction;
private FragmentManager fragmentManager;
private View tabIndicator1;
private View tabIndicator2;
private View tabIndicator3;
@Override
protected void onCreate(final Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_tabs);
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();
mContainerId=R.id.realtabcontent;
fragmentManager = getSupportFragmentManager();
tabIndicator1 = LayoutInflater.from(this).inflate(R.layout.tab, mTabHost.getTabWidget(), false);
tabIndicator2 = LayoutInflater.from(this).inflate(R.layout.tab, mTabHost.getTabWidget(), false);
tabIndicator3 = LayoutInflater.from(this).inflate(R.layout.tab, mTabHost.getTabWidget(), false);
TextView tv1=(TextView)tabIndicator1.findViewById(R.id.txt);
TextView tv2=(TextView)tabIndicator2.findViewById(R.id.txt);
TextView tv3=(TextView)tabIndicator3.findViewById(R.id.txt);
tv1.setText("Tab1");
tv2.setText("Tab2");
tv3.setText("Tab3");
mTabHost.addTab(mTabHost.newTabSpec("1")
.setContent(new DummyTabFactory(this))
.setIndicator(tabIndicator1)
);
mTabHost.addTab(mTabHost.newTabSpec("2")
.setContent(new DummyTabFactory(this))
.setIndicator(tabIndicator2)
);
mTabHost.addTab(mTabHost.newTabSpec("3")
.setContent(new DummyTabFactory(this))
.setIndicator(tabIndicator3)
);
mTabHost.setOnTabChangedListener(new OnTabChangeListener()
@Override
public void onTabChanged(String selectedTabID)
int tabIndex=Integer.valueOf(selectedTabID);
switch(tabIndex)
case 1:
selectedTabID= tabIndicator1.getTag()==null?"Fragment1":tabIndicator1.getTag().toString();
break;
case 2:
selectedTabID= tabIndicator2.getTag()==null?"Fragment2":tabIndicator2.getTag().toString();
break;
case 3:
selectedTabID= tabIndicator3.getTag()==null?"Fragment3":tabIndicator3.getTag().toString();
break;
;
Fragment fragment=fragmentManager.findFragmentByTag(selectedTabID);
if(fragment==null)
fragment=getFragment(selectedTabID);
replaceFragment(fragment,selectedTabID);
);
renderDefaultTab();
public void clickMe(final View view)
Fragment fragment=new AnotherFragment();
replaceFragment(fragment,"AnotherFragment");
@Override
public void replaceFragment(final Fragment fragment, final String tag)
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(mContainerId, fragment,tag);
fragmentTransaction.addToBackStack(tag);
fragmentTransaction.commit();
public void renderDefaultTab()
Fragment fragment=getFragment("Fragment1");
replaceFragment(fragment,"Fragment1");
public Fragment getFragment(final String tag)
Fragment fragment=null;
if(tag.equalsIgnoreCase("Fragment1"))
fragment=new Fragment1();
else if(tag.equalsIgnoreCase("Fragment2"))
fragment=new Fragment2();
else if(tag.equalsIgnoreCase("Fragment3"))
fragment=new Fragment3();
return fragment;
@Override
public void addToTab1Navigation(final String tag)
tabIndicator1.setTag(tag);
@Override
public void addToTab2Navigation(final String tag)
tabIndicator2.setTag(tag);
@Override
public void addToTab3Navigation(final String tag)
tabIndicator3.setTag(tag);
@Override
public void onBackPressed()
String name=fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount()-1).getName();
Fragment fragment=fragmentManager.findFragmentByTag(name);
if(fragment instanceof BaseFragment)
String tag=((BaseFragment)fragment).getPreceddingFragmentTag();
if(tag.equalsIgnoreCase("exit"))
System.exit(0);
else
fragment=fragmentManager.findFragmentByTag(tag);
replaceFragment(fragment, tag);
以及此 MainActivity 的布局。
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_
android:layout_>
<LinearLayout
android:orientation="vertical"
android:layout_
android:layout_>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_
android:layout_
android:layout_weight="0"/>
<FrameLayout
android:id="@+android:id/realtabcontent"
android:layout_
android:layout_
android:layout_weight="1"/>
<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_
android:layout_
android:layout_weight="0"
android:layout_gravity="bottom"/>
</LinearLayout>
</TabHost>
片段1:
package com.example.tabs;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import com.actionbarsherlock.sample.fragments.R;
public class Fragment1 extends BaseFragment
@Override
public void onCreate(final Bundle savedInstanceState)
super.onCreate(savedInstanceState);
public String toString()
return "Fragment1";
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState)
View view=inflater.inflate(R.layout.fragment1, container, false);
return view;
@Override
public void onViewCreated(final View view, final Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
CustomAdapter adapt=new CustomAdapter(getActivity(),0);
ListView lv=(ListView)view.findViewById(R.id.mylistview);
lv.setAdapter(adapt);
FragmentChangeListener fc=(FragmentChangeListener)getActivity();
fc.addToTab1Navigation(this.toString());
lv.setOnItemClickListener(new OnItemClickListener()
@Override public void onItemClick(final AdapterView<?> arg0, final View arg1, final int position, final long arg3)
Fragment fr=new CustomFragment();
Bundle bundle=new Bundle();
bundle.putString("response", "Option "+(position+1));
fr.setArguments(bundle);
FragmentChangeListener fc=(FragmentChangeListener)getActivity();
fc.replaceFragment(fr,"CustomFragment");
);
@Override
public String getPreceddingFragmentTag()
return "exit";
片段2:
package com.example.tabs;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.actionbarsherlock.sample.fragments.R;
public class Fragment2 extends BaseFragment
@Override
public void onCreate(final Bundle savedInstanceState)
super.onCreate(savedInstanceState);
public String toString()
return "Fragment2";
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState)
View view=inflater.inflate(R.layout.fragment2, container, false);
FragmentChangeListener fc=(FragmentChangeListener)getActivity();
fc.addToTab2Navigation(this.toString());
return view;
@Override
public String getPreceddingFragmentTag()
return "exit";
片段3:
package com.example.tabs;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.actionbarsherlock.sample.fragments.R;
public class Fragment3 extends BaseFragment
@Override
public void onCreate(final Bundle savedInstanceState)
super.onCreate(savedInstanceState);
public String toString()
return "Fragment3";
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState)
View view=inflater.inflate(R.layout.fragment3, container, false);
FragmentChangeListener fc=(FragmentChangeListener)getActivity();
fc.addToTab3Navigation(this.toString());
return view;
@Override
public String getPreceddingFragmentTag()
return "exit";
FragmentChangeListener 接口。
package com.example.tabs;
import android.support.v4.app.Fragment;
public interface FragmentChangeListener
public void replaceFragment(final Fragment fragment, final String tag);
public void addToTab1Navigation(String tag);
public void addToTab2Navigation(String tag);
public void addToTab3Navigation(String tag);
DummyTabFactory:
package com.example.tabs;
import android.content.Context;
import android.view.View;
import android.widget.TabHost;
class DummyTabFactory implements TabHost.TabContentFactory
private final Context mContext;
public DummyTabFactory(final Context context)
mContext = context;
@Override
public View createTabContent(final String tag)
View v = new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
自定义片段:
package com.example.tabs;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.actionbarsherlock.sample.fragments.R;
public class CustomFragment extends BaseFragment
@Override
public void onCreate(final Bundle savedInstanceState)
super.onCreate(savedInstanceState);
public String toString()
return "CustomFragment";
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState)
FragmentChangeListener fc=(FragmentChangeListener)getActivity();
fc.addToTab1Navigation(this.toString());
View view=inflater.inflate(R.layout.custom_fragment, container, false);
TextView tv=(TextView)view.findViewById(R.id.response);
tv.setText("You Clicked "+getArguments().getString("response"));
return view;
@Override
public String getPreceddingFragmentTag()
return "Fragment1";
自定义适配器:
package com.example.tabs;
import com.actionbarsherlock.sample.fragments.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.app.Activity;
public class CustomAdapter extends ArrayAdapter<String>
Context mcontext;
public CustomAdapter(final Context context, final int textViewResourceId)
super(context, textViewResourceId);
mcontext=context;
@Override
public int getCount()
return 50;
@Override
public View getView(final int position, final View convertView, final ViewGroup parent)
View row;
if(convertView==null)
LayoutInflater inflater = ((Activity)mcontext).getLayoutInflater();
row = inflater.inflate(R.layout.row, parent, false);
else
row=convertView;
TextView tv=(TextView)row.findViewById(R.id.textView1);
tv.setText("Option "+(position+1));
return row;
基本片段:
package com.example.tabs;
import com.actionbarsherlock.app.SherlockFragment;
public abstract class BaseFragment extends SherlockFragment
public abstract String getPreceddingFragmentTag();
AnotherFragment:
package com.example.tabs;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.actionbarsherlock.sample.fragments.R;
public class AnotherFragment extends BaseFragment
@Override
public void onCreate(final Bundle savedInstanceState)
super.onCreate(savedInstanceState);
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState)
FragmentChangeListener fc=(FragmentChangeListener)getActivity();
fc.addToTab1Navigation(this.toString());
View view=inflater.inflate(R.layout.another_fragment, container, false);
return view;
@Override
public String toString()
return "AnotherFragment";
@Override
public String getPreceddingFragmentTag()
return "CustomFragment";
AnotherFragment 的布局。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_ >
<TextView
android:id="@+id/response"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/another"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
CustomFragment 的布局:
*<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_ >
<TextView
android:id="@+id/response"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_
android:layout_
android:layout_below="@+id/response"
android:text="@string/another_fragment"
android:onClick="clickMe" />
</RelativeLayout>*
Frame1 的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical">
<ListView
android:id="@+id/mylistview"
android:layout_
android:layout_>
</ListView>
</LinearLayout>
Fragment2 的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_ >
<TextView
android:id="@+id/textView1"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/some_text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Fragment3 的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_ >
<ImageView
android:id="@+id/imageView1"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/icon"
android:contentDescription="@string/desc" />
</RelativeLayout>
Row.xml 的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:padding="5dp" />
</RelativeLayout>
Tab.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".MainActivity"
android:layout_margin="3dp"
android:background="@drawable/two_state_button"
android:layout_weight="1">
<ImageView
android:id="@+id/img"
android:layout_
android:layout_
android:src="@drawable/ic_compose_inverse"
android:layout_centerHorizontal="true"
android:contentDescription="@string/desc"/>
<TextView
android:id="@+id/txt"
android:layout_
android:layout_
android:layout_below="@+id/img"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<menu xmlns:android="http://schemas.android.com/apk/res/android"></menu>
【讨论】:
您好,感谢您提供的代码...让我先试试这个.....完成后我会及时通知您。 嗨,艾哈迈德,谢谢,它非常适合我。这是我正在寻找的确切解决方案...【参考方案2】:我的建议是你应该使用新的活动而不是碎片。片段会随你当前的活动一起显示,你不需要关心标签。
但如果您需要使用 Activity,则在 newActivity 布局中创建 tabhost 以及在此 Activity 的布局中创建。 并在创建另一个 Activity 的意图时,将当前的 Tab 编号附加到新的 Activity。
Intent i = new Intent(Activity.this, NewActivity.Class);
i.putIntExtra("CurrentVisibleTab", currentTabIndex);
startActivity(i).
在新活动中。
tabHost.setDefault(getIntent().getIntExtra("CurrentVisibleTab"));
希望这会有所帮助。
【讨论】:
嗨 Ahmad,实际上我需要一个基于标签的导航应用程序,它在每个新布局上都有标签,无论是新的活动还是片段...... 你好,Raj,你应该在 Activity 上新建 Fragment。请参阅文档developer.android.com/guide/components/fragments.html。 你可以在Activity中做任何你想做的事情。不用创建新的 Activity,只需用新的 Fragment 替换当前的 Fragment。以上是关于如何在每个活动中保持标签可见的主要内容,如果未能解决你的问题,请参考以下文章
推送到另一个视图后,如何让这个 ios 按钮在提要视图中保持可见?