把tabhost片段里面呢? [英] Put a tabhost inside a fragment?
问题描述
我尝试DESIN屏幕,这就是这个样子
一个片段的布局https://dl.dropboxusercontent.com/u/37599516/Untitled.png
或者这样的Instagram的
的https://dl.dropboxusercontent.com/u/37599516/Screenshot%202014-01-09%2010.27.19.png
我尝试这个code:
<的LinearLayout
机器人:ID =@ + ID /缩略图
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentLeft =真
机器人:layout_below =@ + ID / relativeLayout1
机器人:layout_marginRight =5dip
机器人:背景=@绘制/ image_bg
机器人:填充=3dip> < ImageView的
机器人:ID =@ + ID / userAvatar
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =89dp
机器人:SRC =@绘制/ test_avatar/>
< / LinearLayout中><的LinearLayout
机器人:ID =@ + ID / linearLayout2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignBottom =@ + ID /缩略图
机器人:layout_alignParentRight =真
机器人:layout_alignTop =@ + ID /缩略图
机器人:layout_below =@ + ID / relativeLayout1
机器人:layout_toRightOf =@ + ID /缩略图
机器人:方向=垂直> <的TextView
机器人:ID =@ + ID / textView1
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:文字=soloco大刀
机器人:textAppearance =机器人:ATTR / textAppearanceMedium/> <的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT> < ImageView的
机器人:ID =@ + ID / ImageView01
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:layout_weight =1
机器人:SRC =@绘制/ description_section_icon/> <的TextView
机器人:ID =@ + ID / textView2
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:layout_weight =1
机器人:重力=center_vertical
机器人:文字=123456
机器人:textAppearance =:/>中的Android ATTR / textAppearanceSmall? < ImageView的
机器人:ID =@ + ID / imageView1
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:layout_weight =1
机器人:SRC =@绘制/ ic_menu_add/>
< / LinearLayout中> <的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT> <按钮
机器人:ID =@ + ID /按钮1
风格=机器人:ATTR / buttonStyleSmall
机器人:layout_width =123dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:文字=按钮/> <按钮
机器人:ID =@ + ID /按钮2
风格=机器人:ATTR / buttonStyleSmall
机器人:layout_width =125dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:文字=按钮/>
< / LinearLayout中>
< / LinearLayout中><的LinearLayout
机器人:ID =@ + ID / linearLayout1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentLeft =真
机器人:layout_alignParentRight =真
机器人:layout_below =@ + ID /缩略图> <的TextView
机器人:ID =@ + ID / textView3
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =match_parent
机器人:重力=center_vertical | CENTER_HORIZONTAL
机器人:文字=馕Đà
机器人:textAppearance =:/>中的Android ATTR / textAppearanceLarge? <的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:layout_weight =1
机器人:方向=垂直> <的TextView
机器人:ID =@ + ID / textView4
机器人:layout_width =match_parent
机器人:layout_height =68dp
机器人:重力=CENTER_HORIZONTAL | center_vertical
机器人:文字=激活
机器人:textAppearance =机器人:ATTR / textAppearanceMedium/> <的TextView
机器人:ID =@ + ID / textView5
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:重力=CENTER_HORIZONTAL | center_vertical
机器人:文字=6/1 / 2013-7 / 1/2013」
机器人:textAppearance =:/>中的Android ATTR / textAppearanceSmall?
< / LinearLayout中>
< / LinearLayout中>
<的LinearLayout
机器人:ID =@ + ID / containerTabhost
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_above =@ + ID / relativeLayout2
机器人:layout_alignParentLeft =真
机器人:layout_alignParentRight =真
机器人:layout_below =@ + ID / linearLayout1
机器人:背景=@绘制/ backgroundactionbar
机器人:方向=垂直>
< TabHost
机器人:ID =@ + ID / tabhost
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:layout_weight =1.30> <的LinearLayout
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直> < TabWidget
机器人:ID =@机器人:ID /标签
机器人:layout_width =match_parent
机器人:layout_height =92dp>
< / TabWidget> <的FrameLayout
机器人:ID =@机器人:ID / tabcontent
机器人:layout_width =match_parent
机器人:layout_height =match_parent> <的LinearLayout
机器人:ID =@ + ID / TAB1
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直> < ListView控件
机器人:ID =@ + ID / tab1list_place_list
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:分=#b5b5b5
机器人:dividerHeight =1DP
机器人:listSelector =@绘制/ list_selector>
< /&的ListView GT;
< / LinearLayout中> <的LinearLayout
机器人:ID =@ + ID / TAB2
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直> < ListView控件
机器人:ID =@ + ID / tab2list_place_list
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:分=#b5b5b5
机器人:dividerHeight =1DP
机器人:listSelector =@绘制/ list_selector/>
< / LinearLayout中> <的LinearLayout
机器人:ID =@ + ID / TAB3
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直> < ListView控件
机器人:ID =@ + ID / tab3list_place_list
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:分=#b5b5b5
机器人:dividerHeight =1DP
机器人:listSelector =@绘制/ list_selector/>
< / LinearLayout中>
< /&的FrameLayout GT;
< / LinearLayout中>
< / TabHost>
< / LinearLayout中>
和使用该code在我的片段类标签主机配合使用
公共类HostFragment扩展片段{
私人FragmentTabHost mTabHost;公共HostFragment(){
//为子片段需要空的构造
}公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
查看rootView = inflater.inflate(R.layout.fragment_host_layout,
集装箱,FALSE); setHasOptionsMenu(真);
TabHost mTabHost =(TabHost)rootView.findViewById(R.id.tabhost); mTabHost.setup();
则tabspec photospec = mTabHost.newTabSpec(搜索);
photospec.setIndicator(搜索,
。getResources()getDrawable(R.drawable.action_search));
意图searchIntent =新意图(getActivity()
HostViewIntroduce.class);
photospec.setContent(searchIntent); 则tabspec songspec = mTabHost.newTabSpec(最爱);
songspec.setIndicator(收藏夹,
。getResources()getDrawable(R.drawable.action_search));
意图favoriteIntent =新意图(getActivity()
HostViewTips.class);
songspec.setContent(favoriteIntent); mTabHost.addTab(songspec);
mTabHost.addTab(photospec); 返回rootView;}@覆盖
公共无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState);}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
// TODO自动生成方法存根
返回super.onOptionsItemSelected(项目);
} }
和它抛出空指针异常
13 01-09:40:31.781:E / AndroidRuntime(5871):致命异常:主要
01-09 13:40:31.781:E / AndroidRuntime(5871):java.lang.IllegalStateException:你忘了叫公共无效设置(LocalActivityManager的ActivityGroup)?
您需要使用的 FragmentTabHost 的的自定义的的 addTab() 这也会把你的片段类作为参数。</ p>
替换
则tabspec photospec = mTabHost.newTabSpec(搜索);
photospec.setIndicator(搜索,
。getResources()getDrawable(R.drawable.action_search));
意图searchIntent =新意图(getActivity()
HostViewIntroduce.class);
photospec.setContent(searchIntent); 则tabspec songspec = mTabHost.newTabSpec(最爱);
songspec.setIndicator(收藏夹,
。getResources()getDrawable(R.drawable.action_search));
意图favoriteIntent =新意图(getActivity()
HostViewTips.class);
songspec.setContent(favoriteIntent);
与
TabHost.TabSpec photospec = mTabHost.newTabSpec(搜索);
photospec.setIndicator(搜索,
。getResources()getDrawable(R.drawable.action_search));
photospec.setIndicator(rootView);
mTabHost.addTab(photospec,HostViewIntroduce.class,NULL);
则tabspec songspec = mTabHost.newTabSpec(最爱);
songspec.setIndicator(收藏夹,
。getResources()getDrawable(R.drawable.action_search));
songspec.setIndicator(rootView);
mTabHost.addTab(songspec,HostViewTips.class,NULL);
I try to desin a screen , which is a layout of a fragment like this
https://dl.dropboxusercontent.com/u/37599516/Untitled.png
Or like this in instagram
https://dl.dropboxusercontent.com/u/37599516/Screenshot%202014-01-09%2010.27.19.png
i try this code :
<LinearLayout
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/relativeLayout1"
android:layout_marginRight="5dip"
android:background="@drawable/image_bg"
android:padding="3dip" >
<ImageView
android:id="@+id/userAvatar"
android:layout_width="wrap_content"
android:layout_height="89dp"
android:src="@drawable/test_avatar" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/thumbnail"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/thumbnail"
android:layout_below="@+id/relativeLayout1"
android:layout_toRightOf="@+id/thumbnail"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="soloco bolo"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:src="@drawable/description_section_icon" />
<TextView
android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="123456"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:src="@drawable/ic_menu_add" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="123dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/thumbnail" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:text="Đà Nẵng"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="68dp"
android:gravity="center_horizontal|center_vertical"
android:text="Activate"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:text="6/1/2013-7/1/2013"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/containerTabhost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/relativeLayout2"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout1"
android:background="@drawable/backgroundactionbar"
android:orientation="vertical" >
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.30" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="92dp" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/tab1list_place_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/tab2list_place_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/tab3list_place_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
and using this code to work with tab host in my fragment class
public class HostFragment extends Fragment {
private FragmentTabHost mTabHost;
public HostFragment() {
// Empty constructor required for fragment subclasses
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_host_layout,
container, false);
setHasOptionsMenu(true);
TabHost mTabHost = (TabHost) rootView.findViewById(R.id.tabhost);
mTabHost.setup( );
TabSpec photospec = mTabHost.newTabSpec("Search");
photospec.setIndicator("Search",
getResources().getDrawable(R.drawable.action_search));
Intent searchIntent = new Intent(getActivity(),
HostViewIntroduce.class);
photospec.setContent(searchIntent);
TabSpec songspec = mTabHost.newTabSpec("Favorites");
songspec.setIndicator("Favorites",
getResources().getDrawable(R.drawable.action_search));
Intent favoriteIntent = new Intent(getActivity(),
HostViewTips.class);
songspec.setContent(favoriteIntent);
mTabHost.addTab(songspec);
mTabHost.addTab(photospec);
return rootView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
return super.onOptionsItemSelected(item);
}
}
and it throw nullpointer exception
01-09 13:40:31.781: E/AndroidRuntime(5871): FATAL EXCEPTION: main
01-09 13:40:31.781: E/AndroidRuntime(5871): java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
You need to use FragmentTabHost's custom addTab() which will also take your Fragment class as parameter.
Replace
TabSpec photospec = mTabHost.newTabSpec("Search");
photospec.setIndicator("Search",
getResources().getDrawable(R.drawable.action_search));
Intent searchIntent = new Intent(getActivity(),
HostViewIntroduce.class);
photospec.setContent(searchIntent);
TabSpec songspec = mTabHost.newTabSpec("Favorites");
songspec.setIndicator("Favorites",
getResources().getDrawable(R.drawable.action_search));
Intent favoriteIntent = new Intent(getActivity(),
HostViewTips.class);
songspec.setContent(favoriteIntent);
with
TabHost.TabSpec photospec = mTabHost.newTabSpec("Search");
photospec.setIndicator("Search",
getResources().getDrawable(R.drawable.action_search));
photospec.setIndicator(rootView);
mTabHost.addTab(photospec, HostViewIntroduce.class, null);
TabSpec songspec = mTabHost.newTabSpec("Favorites");
songspec.setIndicator("Favorites",
getResources().getDrawable(R.drawable.action_search));
songspec.setIndicator(rootView);
mTabHost.addTab(songspec, HostViewTips.class, null);
这篇关于把tabhost片段里面呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!